Jump to content

Regular expression absorbes @CRLFs


Recommended Posts

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 Midget
Line 2 Midget  1 vs VYY Stars

+++: $sTextFixed ===>
Line 1 MF +++vs NE +++Line 2 +++ vs VYY Stars
 

 

Edited by AndyS01
Link to comment
Share on other sites

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

  • Developers

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

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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

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 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

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

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

×
×
  • Create New...