Sign in to follow this  
Followers 0
litlmike

Replacing Whole Words Only (String)

18 posts in this topic

I would like to replace Whole Words only, not just any substring. For instance, if I want to replace the string "and", and I have the word "hand", StringReplace() outputs "h", I would like the output to still be "hand". Again, if I have "Hand and Foot", I would like to replace "and" with "&", I would like to have the output result in "Hand & Foot", not "H& & Foot". Below is an example of what currently takes place.

Thanks in Advance.

$String = "Hand and Foot"
$SearchString = "and"
$ReplaceString = "&"
MsgBox (0, "Test", "Original: " & $String & @CRLF & "Replaced: " & StringReplace ($String, $SearchString, $ReplaceString))

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I would like to replace Whole Words only, not just any substring. For instance, if I want to replace the string "and", and I have the word "hand", StringReplace() outputs "h", I would like the output to still be "hand". Again, if I have "Hand and Foot", I would like to replace "and" with "&", I would like to have the output result in "Hand & Foot", not "H& & Foot". Below is an example of what currently takes place.

Thanks in Advance.

$String = "Hand and Foot"
$SearchString = "and"
$ReplaceString = "&"
MsgBox (0, "Test", "Original: " & $String & @CRLF & "Replaced: " & StringReplace ($String, $SearchString, $ReplaceString))

But in for more general cases you will probably need StringRegExpReplace.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Hi, try this my function...

$String = 'Hand and Foot'
$Search = "and"
$Replace = "&"

$NewStr = _StringReplaceEx($String, $Search, $Replace, 3)

MsgBox(0, "Results", "New string is: " & $NewStr & @LF & "Replacments in string: " & @extended)

Func _StringReplaceEx($String, $SearchStr, $ReplaceStr, $Flag=0, $Count=0, $CaseSense=0)
    If $Flag <> 0 Then
        Switch $Flag
            Case 1
                If StringLeft($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, " " & $SearchStr, " " & $ReplaceStr, $Count, $CaseSense)
                EndIf
            Case 2
                If StringRight($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, $SearchStr & " ", $ReplaceStr & " ", $Count, $CaseSense)
                EndIf
            Case 3
                If StringLeft($String, StringLen($SearchStr)) = $SearchStr Or _
                        StringRight($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, " " & $SearchStr & " ", " " & $ReplaceStr & " ", $Count, $CaseSense)
                EndIf
            Case Else
                Return SetError(3, 0, $String)
        EndSwitch
    Else
        $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
    EndIf
    Return SetError(0, @extended, $String)
EndFunc

Flag = 0 - Will replace as standard StringReplace Function.

Flag = 1 - Will replace only if the founded string is whole word from left side

Flag = 2 - Will replace only if the founded string is whole word from right side

Flag = 3 - Will replace only if the founded string is whole word from bouth sides


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Or using RegExp...

Func _StringReplaceEx($String, $SearchStr, $ReplaceStr, $Flag=0, $Count=0, $CaseSense=0)
    If $Flag <> 0 Then
        Local $CaseSensePrefix = "(?i)"
        If $CaseSense = 1 Then $CaseSensePrefix = ""
        Switch $Flag
            Case 1
                $String = StringRegExpReplace($String, $CaseSensePrefix & "\s" & $SearchStr, " " & $ReplaceStr, $Count)
            Case 2
                $String = StringRegExpReplace($String, $CaseSensePrefix & "" & $SearchStr & "\s", $ReplaceStr & " ", $Count)
            Case 3
                $String = StringRegExpReplace($String, $CaseSensePrefix & "\s" & $SearchStr & "\s", " " & $ReplaceStr & " ", $Count)
            Case Else
                Return SetError(3, 0, $String)
        EndSwitch
    Else
        $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
    EndIf
    Return SetError(0, @extended, $String)
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Hi, try this my function...

$String = 'Hand and Foot'
$Search = "and"
$Replace = "&"

$NewStr = _StringReplaceEx($String, $Search, $Replace, 3)

MsgBox(0, "Results", "New string is: " & $NewStr & @LF & "Replacments in string: " & @extended)

Func _StringReplaceEx($String, $SearchStr, $ReplaceStr, $Flag=0, $Count=0, $CaseSense=0)
    If $Flag <> 0 Then
        Switch $Flag
            Case 1
                If StringLeft($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, " " & $SearchStr, " " & $ReplaceStr, $Count, $CaseSense)
                EndIf
            Case 2
                If StringRight($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, $SearchStr & " ", $ReplaceStr & " ", $Count, $CaseSense)
                EndIf
            Case 3
                If StringLeft($String, StringLen($SearchStr)) = $SearchStr Or _
                        StringRight($String, StringLen($SearchStr)) = $SearchStr Then
                    $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
                Else
                    $String = StringReplace($String, " " & $SearchStr & " ", " " & $ReplaceStr & " ", $Count, $CaseSense)
                EndIf
            Case Else
                Return SetError(3, 0, $String)
        EndSwitch
    Else
        $String = StringReplace($String, $SearchStr, $ReplaceStr, $Count, $CaseSense)
    EndIf
    Return SetError(0, @extended, $String)
EndFunc

Good idea but it needs some changes. Maybe the default $Count should be 1 not 0.

If you change the search string to "and hand foot" it will all go wrong.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Better with StringRegExpReplace() as martin said.

It can also be done using _FileReadToArray() and StringSplit()

$F_Array = ""
_FileReadToArray($File, $F_Array)
For $I = 1 To Ubound($F_Array)-1
 $Line = StringSplit($F_Array[$I], Chr(32))
    $O_Line = ""
    For $J = 1 To $Line[0]
       If $Line[$J] = "and" Then $Line[$J] = "Replace"
       $O_Line &= $Line[$J] & Chr(32)
    Next
    $F_Array[$I] = $O_Line
Next

And then write $F_Array to $File

Edit: The word to search for and the replacement strings can also be sent as variables

$S = "and"
$R = "replace"

In the second loop use

If $Line[$J] = $S Then $LIne[$J] = $R

Another method is to do a StringReplace() that includes the white space so you don't need the second loop

$F_Array[$I] = StringReplace($F_Array[$I], " and ", " or ")

But that will fail in cases where there is no leading and trailing white space.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

This will work as well...

#include <Word.au3>
$oWordApp = _WordCreate(@ScriptDir & "\Test.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oDoc.Range.Text = "Hand and Foot"
$oFind = _WordDocFindReplace($oDoc, "and", "&", 2, 0, 0, 1)

Share this post


Link to post
Share on other sites

I'm no Regex expert, but doesn't this work?

$test = "This is my test text. It is about a hand and a foot. The " & @LF _
    & "hand wanted to be a head, " & @LF _
    & "and, also, the foot wanted the " & @LF _
    & "word 'and' tattooed on it. Theirs was a strange relationship."

MsgBox(0, "RegEx Replace Test", StringRegExpReplace($test, "\band\b", "&"))

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I'm no Regex expert, but doesn't this work?

$test = "This is my test text. It is about a hand and a foot. The " & @LF _
    & "hand wanted to be a head, " & @LF _
    & "and, also, the foot wanted the " & @LF _
    & "word 'and' tattooed on it. Theirs was a strange relationship."

MsgBox(0, "RegEx Replace Test", StringRegExpReplace($test, "\band\b", "&"))
What we are trying to show is that there are multiple methods of doing it.

RegExReplace was mentioned as the better method to use but some people are not comfortable with RegEx anything.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Maybe the default $Count should be 1 not 0.

Why? if it 0 then all intance will be replaced, just as in standard StringReplace function.

See my last version.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

RegExReplace was mentioned as the better method to use but some people are not comfortable with RegEx anything.

I hear that. I struggle through it every time I need to use it.

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

Why? if it 0 then all intance will be replaced, just as in standard StringReplace function.

See my last version.

Only because, as I said, with your function to result of

$String = 'and so I handed the wand to Mandy'
$Search = "and"
$Replace = "&"

$NewStr = _StringReplaceEx($String, $Search, $Replace, 3)

is

"& so I h&ed the w$ to M&y" which is not what was wanted.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Hmmm, this is quite interesting to see the differences of opinions here. I really thought that this would a clear cut issue that many have dealt with in the past, even though I couldn't find much by using "Search". It seems that StringRegEx will work as long as I specify a blank space (" ") before/after the word I am replacing?

Funny enough big_daddy's suggestion works nicely with the rest of the script I am using. However, is there a case where StringRegExpReplace will not work?

Share this post


Link to post
Share on other sites

However, is there a case where StringRegExpReplace will not work?

Not as far as I can foresee.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

which is not what was wanted

I get this (with LAST VERSION):

and so I handed the wand to Mandy

If you want to replace only the "and ", then you must use $Flag = 2 and $Count = 1...

$String = 'and so I handed the wand to Mandy'
$Search = "and"
$Replace = "&"

$NewStr = _StringReplaceEx($String, $Search, $Replace, 2, 1)
ConsoleWrite($NewStr)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

I get this (with LAST VERSION):

If you want to replace only the "and ", then you must use $Flag = 2 and $Count = 1...

$String = 'and so I handed the wand to Mandy'
$Search = "and"
$Replace = "&"

$NewStr = _StringReplaceEx($String, $Search, $Replace, 2, 1)
ConsoleWrite($NewStr)
Some words aren't always just separated by spaces, you may want to see this thread: http://www.autoitscript.com/forum/index.ph...st&p=378823 and see the regexp that will more than likely be the closest for finding words.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Some words aren't always just separated by spaces

But if we search for "Whole word Only", that means that eather this word is founded like this: "..... Word .....", eather like this: "Word ....." (or also like this: ".... Word").

So for what this topic is about, i think my function is good enouth :).


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Just wanted to point out my example earlier. In it, '\b' is recognized by the StringRegExpReplace function as a word boundary, so it will only substitute '&' for 'and' if it is a whole word. Apparently, the '\b' anchor has a lot of intelligence in it regarding what constitutes a word boundary. This includes if the word 'and' is at the beginning of the line, at the end of a sentence, and surrounded by punctuation. In all of these circumstances, using the string ' and ' will fail.

In short, this code:

$test = "This is my test text. It is about a hand and a foot. The " & @LF _
    & "hand wanted to be a head, " & @LF _
    & "and, also, the foot wanted the " & @LF _
    & "word 'and' tattooed on it. Theirs was a strange relationship."

MsgBox(0, "RegEx Replace Test", StringRegExpReplace($test, "\band\b", "&"))

returns this result:

This is my test text. It is about a hand & a foot. The

hand wanted to be a head,

&, also, the foot wanted the

word '&' tattooed on it. Theirs was a strange relationship.

Note that the 'and's at the beginning of the line and with single quotes around them got properly substituted, but the 'hand's were not. Edited by bluebearr

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
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
Sign in to follow this  
Followers 0