Jump to content
Sign in to follow this  
TripleDES

Help with StringRegExp and (?s)

Recommended Posts

TripleDES

I'm trying to read a file and match characters that include multiple (new) lines...Example:

"The flag parameter

can have one of

5 values (0 through 4)."

So I'd like to match anything inside of "parameter" and "5" in this example. I get a successful match using parameter([\s\S]*?)5, but not while using (?s). The following fails: parameter((?s)*?)5 Anything else I can try?

Share this post


Link to post
Share on other sites
GEOSoft

Anything else I can try?

Lot's of things but what do you consider a successful return to be?

Is this the only example you want to give us or are there more that we would have to come back and make changes to accomodate?

When posting RegEx questions. Always include a few examples of the input strings and a good representation of what you expect from each one.

Edit: BTW, the second regex isn't going to work no matter what you want returned. It's totally incorrect.

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
GEOSoft

For the example you posted, this will work

$sStr = "The flag parameter" & @CRLF
$sStr &= "can have one of" & @CRLF
$sStr &= "5 values (0 through 4)."
$aRtn = StringRegExp($sStr, "(?s)(?i)parameter(?:\v+)(.+?)(?:\v+)5.*", 1)
If NOT @Error Then MsgBox(0, "Result", $aRtn[0])

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
TripleDES

Lot's of things but what do you consider a successful return to be?

Flag 0....simply return a 1 when a match is found.

Edit: BTW, the second regex isn't going to work no matter what you want returned. It's totally incorrect.

Perhaps I've misunderstood the purpose of "(?s)". I interpreted it to to also match CR/LF characters where standard "." will not.

$aRtn = StringRegExp($sStr, "(?s)(?i)parameter(?:\v+)(.+?)(?:\v+)5.*", 1)

Thank you! Based on this example, "\v" looks to be more appropriate than "(?s)" in order to match CR/LF. I'll be sure to add more clarity the next time I have a regex question.

Share this post


Link to post
Share on other sites
GEOSoft

All that was really wrong with parameter((?s)*?)5 was a missing dot

parameter((?s).*?)5

But I also assumed you didn't want any vertical white space in there either. And since you were testing for True/False Only it can be even easier

If StringRegExp($sStr, "(?i)(?s)parameter(.+?)5") Then

Or even

"(?i)(?s)parameter.*?([\w\h]+?)5"

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
TripleDES

All that was really wrong with parameter((?s)*?)5 was a missing dot

parameter((?s).*?)5

I tried this after my initial post, it worked, but I wasn't sure if it was working as intended (if that makes any sense). With my limited regex knowledge, here's how I interpreted "((?s).*?)".

(?s) = Match any single character (including newline)

.*? = Non-greedy match any character except newline 0 or more times

Therefore, I thought I was getting a false positive result because it would match a single character with "(?s)" and the rest with ".*?" but what I was trying to do was achieve the following:

??? = Non-greedy match any character including newline 0 or more times

I tried to replace "." with "(?s)", which is why I was going down the incorrect path of: "(?s)*?" :idea:

Thanks again!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.