AndyS01 Posted May 28, 2019 Share Posted May 28, 2019 (edited) I have a regular expression that I use in a StringRegExpReplace() function that replaces CRLFs if the matching text is the last text on a line. Here is my example script: test() Exit (1) Func test() Local $sfor, $pat, $sTextBefore = "", $sTextFixed $sTextBefore &= "Line 1 MF Midget vs NE Midget" & @CR $sTextBefore &= "Line 2 Midget 1 vs VYY Stars" & @CR $sfor = "Midget" $pat = "(?i)Midget\s{0,}[s]{0,1}\s{0,}[0-9]?+" $sTextFixed = StringRegExpReplace($sTextBefore, $pat, "+++") ConsoleWrite("+++: $pat ====>" & $pat & "<==" & @CRLF) ConsoleWrite("+++: $sTextBefore ==>" & @CRLF & $sTextBefore & @CRLF) ConsoleWrite("+++: $sTextFixed ===>" & @CRLF & $sTextFixed & @CRLF) EndFunc The output looks like this (I can't figure out how to remove the strikeout in this example): Quote +++: $pat ====>(?i)Midget\s{0,}{0,1}\s{0,}[0-9]?+<==+++: $sTextBefore ==>Line 1 MF Midget vs NE MidgetLine 2 Midget 1 vs VYY Stars+++: $sTextFixed ===>Line 1 MF +++vs NE +++Line 2 +++ vs VYY Stars Edited May 28, 2019 by AndyS01 Link to comment Share on other sites More sharing options...
Developers Jos Posted May 28, 2019 Developers Share Posted May 28, 2019 Is this what you want? : $pat = "(?i)Midget\s{0,}?[s]{0,1}?\s{0,}?[0-9]?+" Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
AndyS01 Posted May 28, 2019 Author Share Posted May 28, 2019 Almost. Adding '?' after a match prevents regexp from being greedy? The concept of greedy, lazy and possessive is quite confusing. I looked at some on line tutorials and got my head swimming. My example didn't supply a plural for Midget. I meed to match 'Midget', 'Midgets', 'Midget 1', 'Midgets 1': $sTextBefore &= "Line 1 MF Midgets vs NE Midgets" & @CR $sTextBefore &= "Line 2 Midgets 1 vs VYY Stars" & @CR When I do that, I only match 'Midget', not 'Midgets', like so: test() Exit (1) Func test() Local $sfor, $pat, $sTextBefore = "", $sTextFixed $sTextBefore &= "Line 1 MF Midgets vs NE Midgets" & @CR $sTextBefore &= "Line 2 Midgets 1 vs VYY Stars" & @CR $sfor = "Midget" $pat = "(?i)Midget\s{0,}?[s]{0,1}?\s{0,}?[0-9]?+" $sTextFixed = StringRegExpReplace($sTextBefore, $pat, "+++") ConsoleWrite("+++: $pat ====>" & $pat & "<==" & @CRLF) ConsoleWrite("+++: $sTextBefore ==>" & @CRLF & $sTextBefore & @CRLF) ConsoleWrite("+++: $sTextFixed ===>" & @CRLF & $sTextFixed & @CRLF) EndFunc The output is this (notice the 's' characters remaining in the output): Quote Line 1 MF +++s vs NE +++s Line 2 +++s 1 vs VYY Stars Link to comment Share on other sites More sharing options...
Developers Jos Posted May 28, 2019 Developers Share Posted May 28, 2019 It's always fun these regex's What about this version: $pat = "(?i)(?m)Midgets*[[:blank:]]+[0-9]*[[:blank:]]*" Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
AndyS01 Posted May 28, 2019 Author Share Posted May 28, 2019 Perfect, just what I needed. Thank you. Link to comment Share on other sites More sharing options...
iamtheky Posted May 28, 2019 Share Posted May 28, 2019 (edited) even though it doesnt catch the second instance of midget in Line 1? Quote +++: $pat ====>(?i)(?m)Midgets*[[:blank:]]+[0-9]*[[:blank:]]*<== +++: $sTextBefore ==> Line 1 MF Midget vs NE Midget Line 2 Midget 1 vs VYY Stars +++: $sTextFixed ===> Line 1 MF +++vs NE Midget Line 2 +++vs VYY Stars Here's one minus the regex, just hammers the pieces into place test() Exit (1) Func test() Local $sfor, $pat, $sTextBefore = "", $sTextFixed $sTextBefore &= "Line 1 MF Midget vs NE Midget" & @CR $sTextBefore &= "Line 2 Midget 1 vs VYY Stars" & @CR consolewrite(StringReplace(StringStripCR(StringReplace($sTextBefore , "Midgets" , "+++")) , "Midget" , "+++")) EndFunc Edited May 28, 2019 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
AndyS01 Posted May 29, 2019 Author Share Posted May 29, 2019 Oh-Oh I see what you mean iamtheky. This didn't work: test() Exit (1) Func test() Local $sfor, $pat, $sTextBefore = "", $sTextFixed $sTextBefore &= "Line 1 MF Midget vs NE Midgets" & @CR $sTextBefore &= "Line 2 Midgets 1 vs VYY Stars" & @CR $sfor = "Midget" $pat = "(?i)(?m)Midgets*[[:blank:]]+[0-9]*[[:blank:]]*" $sTextFixed = StringRegExpReplace($sTextBefore, $pat, "+++") ConsoleWrite("+++: $pat ====>" & $pat & "<==" & @CRLF) ConsoleWrite("+++: $sTextBefore ==>" & @CRLF & $sTextBefore & @CRLF) ConsoleWrite("+++: $sTextFixed ===>" & @CRLF & $sTextFixed & @CRLF) EndFunc The output was: Quote +++: $pat ====>(?i)(?m)Midgets*[[:blank:]]+[0-9]*[[:blank:]]*<== +++: $sTextBefore ==> Line 1 MF Midget vs NE Midgets Line 2 Midgets 1 vs VYY Stars +++: $sTextFixed ===> Line 1 MF +++vs NE Midgets Line 2 +++vs VYY Stars Link to comment Share on other sites More sharing options...
AndyS01 Posted May 29, 2019 Author Share Posted May 29, 2019 The 's' in Midgets' is optional. As I said before, I want to replace "Midget", "Midgets", "Midget 1", "Midgets 1" with the "+++" string. Also, the '1' could be [0-9]. Link to comment Share on other sites More sharing options...
jchd Posted May 29, 2019 Share Posted May 29, 2019 Then that should work for you: _test() Exit (1) Func _test() Local $sfor, $pat, $sTextBefore = "", $sTextFixed $sTextBefore &= "Line 1 MF Midget vs NE Midgets" & @CR $sTextBefore &= "Line 2 Midgets 1 vs VYY Stars" & @CR $sfor = "Midget" $pat = "(?im)Midgets?(?: *\d*)?" $sTextFixed = StringRegExpReplace($sTextBefore, $pat, "+++") ConsoleWrite("+++: $pat ====>" & $pat & "<==" & @CRLF) ConsoleWrite("+++: $sTextBefore ==>" & @CRLF & $sTextBefore & @CRLF) ConsoleWrite("+++: $sTextFixed ===>" & @CRLF & $sTextFixed & @CRLF) EndFunc 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
mikell Posted May 29, 2019 Share Posted May 29, 2019 An alternative way allowing variations $sTextBefore = "Line 1 MF Midget vs NE Midgets" & @CR & _ "Line 2 Midgets -1a vs VYY Stars" & @CR $pat = "(?im)Midget.*?(?=\hvs|$)" Msgbox(0,"", StringRegExpReplace($sTextBefore, $pat, "+++")) iamtheky 1 Link to comment Share on other sites More sharing options...
AndyS01 Posted May 29, 2019 Author Share Posted May 29, 2019 2 hours ago, mikell said: An alternative way allowing variations $sTextBefore = "Line 1 MF Midget vs NE Midgets" & @CR & _ "Line 2 Midgets -1a vs VYY Stars" & @CR $pat = "(?im)Midget.*?(?=\hvs|$)" Msgbox(0,"", StringRegExpReplace($sTextBefore, $pat, "+++")) In real live production, the 'vs' might not be there. 2 hours ago, mikell said: An alternative way allowing variations $sTextBefore = "Line 1 MF Midget vs NE Midgets" & @CR & _ "Line 2 Midgets -1a vs VYY Stars" & @CR $pat = "(?im)Midget.*?(?=\hvs|$)" Msgbox(0,"", StringRegExpReplace($sTextBefore, $pat, "+++")) Link to comment Share on other sites More sharing options...
AndyS01 Posted May 29, 2019 Author Share Posted May 29, 2019 The example in jchd's reply worked for all examples that I tried. Thanks Link to comment Share on other sites More sharing options...
mikell Posted May 29, 2019 Share Posted May 29, 2019 5 hours ago, AndyS01 said: In real live production, the 'vs' might not be there. So the question was badly asked. No way to guess that "real life" might be different than the provided example Link to comment Share on other sites More sharing options...
AndyS01 Posted May 29, 2019 Author Share Posted May 29, 2019 True enough. The original question was why the CRLF was being absorbed, joining two lines, The eventual fix was the (?m) part of the RE. Link to comment Share on other sites More sharing options...
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