Jump to content

RegExpReplace Count Incorrect


Guy_
 Share

Go to solution Solved by Melba23,

Recommended Posts

I'll always feel a n00b and therefore hesitant to post what I think is a bug... ;)

Having a first go with this one (that doesn't work in the release version either):

Manual for StringRegExpReplace states:

"Check @extended for the number of replacements performed"

However this seems not to be happening for my _WordCount( $text ) function

and I have to use _WordCount2( $text ) as a workaround.

"I hope this is a real bug"...? ;)

Global $text = "Six word sentence for testing purposes."

MsgBox(0,"", "The sentence  '" & $text & "'  has  " & _WordCount( $text ) & "  words." & @CRLF & _
"The sentence  '" & $text & "'  has  " & _WordCount2( $text ) & "  words." )


; manual states: "Check @extended for the number of replacements performed" (but doesn't seem to work?)
Func _WordCount( $text )
    StringRegExpReplace( $text, "(\s){1,}[\W|_](\s){1,}|\R", " ", 0 )
    Return @extended + 1    ; nr of spaces + 1 = wordcount
EndFunc


; workaround
Func _WordCount2( $text )
    StringReplace( StringRegExpReplace( $text, "(\s){1,}[\W|_](\s){1,}|\R", " ", 0 ), " ", "x")
    Return @extended + 1    ; nr of spaces + 1 = wordcount
EndFunc
Edited by Guy_
Link to comment
Share on other sites

Why make it hard?

Global $text = "A 8-word sentence for 365 testing purposes."

MsgBox(0,"", "The sentence  '" & $text & "'  has  " & _WordCount( $text ) & "  words.")

; manual states: "Check @extended for the number of replacements performed" (but doesn't seem to work?)
Func _WordCount( $text )
    StringRegExpReplace( $text, "\b([[:alnum:]]+)", "")
    Return @extended    ; nbr of words
EndFunc

EDIT: without uderscores allowed inside words.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Moderators
  • Solution

Guy_,

No bug - your RegEx just does not work and so it makes no replacements. ;)

Just add some error checking and you can see what happens: :)

Global $text = "Six word sentence for testing purposes."

MsgBox(0,"", "The sentence  '" & $text & "'  has  " & _WordCount( $text ) & "  words." & @CRLF & _
"The sentence  '" & $text & "'  has  " & _WordCount2( $text ) & "  words." & @CRLF & _
"The sentence  '" & $text & "'  has  " & _WordCount3( $text ) & "  words." & @CRLF & _
"The sentence  '" & $text & "'  has  " & _WordCount4( $text ) & "  words." )

; Your RegEx fails
Func _WordCount( $text )
    $sNewText = StringRegExpReplace( $text, "(\s){1,}[\W|_](\s){1,}|\R", "#", 0 )
    $iExtended = @extended
    ConsoleWrite($sNewText & @CRLF)
    Return $iExtended + 1    ; nr of spaces + 1 = wordcount
EndFunc

; This is functionally equivlent to...
Func _WordCount2( $text )
    $sNewText = StringReplace( StringRegExpReplace( $text, "(\s){1,}[\W|_](\s){1,}|\R", " ", 0 ), " ", "x")
    $iExtended = @extended
    ConsoleWrite($sNewText & @CRLF)
    Return $iExtended + 1    ; nr of spaces + 1 = wordcount
EndFunc

; ...this simple StringReplace
Func _WordCount3( $text )
    $sNewText = StringReplace($text, " ", "x")
    $iExtended = @extended
    ConsoleWrite($sNewText & @CRLF)
    Return $iExtended + 1    ; nr of spaces + 1 = wordcount
EndFunc

; But if you use a RegEx that actually works you get the count returned
Func _WordCount4( $text )
    $sNewText = StringRegExpReplace($text, "(\s+)", "#")
    $iExtended = @extended
    ConsoleWrite($sNewText & @CRLF)
    Return $iExtended + 1    ; nr of spaces + 1 = wordcount
EndFunc
As you can see in the SciTE console, no replacements are made with your RegEx, so the count is correct. ;)

M23

Edit: In future please start a new thread rather than post in the Beta thread which is used to point out specific problems with the Beta rather than generic problems like this. I will split these posts off in a while. :)

Edit 2:

And split. ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you both.

This is a lesson to integrate error checking... :sweating:

Why make it hard?

 

The codes are to filter out some specific stuff that I don't want to be counted as a real word.
And I thought it was working too... (maybe it is in part, just not in this example with only regular spaces)

Edited by Guy_
Link to comment
Share on other sites

  • Moderators

Guy_,

 

This was my last bug report ever!

By all means do report possible bugs - but as I said in your earlier thread, best to post in the forum first to get others to check. :)

And do some in depth errorchecking yourself as well, of course. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...