IchBistTod Posted March 23, 2010 Posted March 23, 2010 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]
logmein Posted March 23, 2010 Posted March 23, 2010 Try this : $text1 = 'dog ate dog2' $replaced = StringReplace ($text1,'dog','the dog',1) ConsoleWrite ($replaced) [font=arial, helvetica, sans-serif][s]Total USB Security 3.0 Beta[/s] | [s]Malware Kill[/s] | Malware Scanner | Screen Hider | Locker | Matrix Generator[s]AUTO-SYNC 1.0 | MD5 Hash Generator | URL Checker | Tube Take [/s]| Random Text[/font]
IchBistTod Posted March 23, 2010 Author Posted March 23, 2010 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]
JRowe Posted March 23, 2010 Posted March 23, 2010 (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 March 23, 2010 by JRowe [center]However, like ninjas, cyber warriors operate in silence.AutoIt Chat Engine (+Chatbot) , Link Grammar for AutoIt , Simple Speech RecognitionArtificial Neural Networks UDF , Bayesian Networks UDF , Pattern Matching UDFTransparent PNG GUI Elements , Au3Irrlicht 2Advanced Mouse Events MonitorGrammar Database GeneratorTransitions & Tweening UDFPoker Hand Evaluator[/center]
IchBistTod Posted March 23, 2010 Author Posted March 23, 2010 (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 March 23, 2010 by IchBistTod [center][/center][center]=][u][/u][/center][center][/center]
GEOSoft Posted March 23, 2010 Posted March 23, 2010 $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!"
IchBistTod Posted March 23, 2010 Author Posted March 23, 2010 (edited) $sReplaced = StringRegExpReplace($sStr, "(?i)\btest\b", "aha")Edit:With a few modifcation, it does work perfect =]/ Edited March 23, 2010 by IchBistTod [center][/center][center]=][u][/u][/center][center][/center]
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now