Sign in to follow this  
Followers 0
IchBistTod

Replace only exact phrase

7 posts in this topic

Okay I want to replace only an exact phrase in a string.

Example:

"dog ate dog2"

When I want to just replace "dog" with "the dog" I get

"the dog ate the dog2"

when I want.

"The dog ate dog2"

So it will only replace if the whole word matches.

Do you understand what I am saying?

Thanks for help.


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites



Try this :

$text1 = 'dog ate dog2'
$replaced = StringReplace ($text1,'dog','the dog',1)
ConsoleWrite ($replaced)

Not acceptable. Sorry.

Text may need to be replaced more than 1 time.

like

"Dog ate dog2 and dog was happy"

then needs to be

"The dog ate dog2 and the dog was happy"

You see better now what I am meaning?


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$text1 = 'dog ate dog2 and dog died of indigestion, dawg.'
$replaced = StringRegExpReplace($text1, "(dog )|( dog )", " the dog ")

ConsoleWrite($replaced)

Something like that. You might have to do two passes.

StringReplace and StringRegExpReplace both work with whitespace. You could refine the regex to work with beginning of the string, and to exclude partial matches inside strings like bulldog and hotdog, if they exist.

Edited by JRowe

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

this will be acceptable

$text1 = ' dog ate dog2 and dog died of indigestion, dawg. Hotdog bulldog.'
$replaced = StringRegExpReplace($text1, "( dog )", " the dog ")

ConsoleWrite($replaced&@CRLF)

as in my code nothign needs replace that is the very first word.

=]

thank you.

I actually make it work this way.

But how would I make it so it only replace exact word like

test

test1

replace with

aha

and have

aha

test1

not

aha

aha1

Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

$sReplaced = StringRegExpReplace($sStr, "(?i)\btest\b", "aha")


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

#7 ·  Posted (edited)

$sReplaced = StringRegExpReplace($sStr, "(?i)\btest\b", "aha")

Edit:

With a few modifcation, it does work perfect =]/

Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

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