Sign in to follow this  
Followers 0
gamepin126

StringRegExp only returning optional group

9 posts in this topic

#1 ·  Posted (edited)

The relevant code's quite large, but this is the primary issue I'm having. I'm using RegexBuddy to test my patterns, and it has no issue returning the value I want.

Test string : "value               795.45    23.57%    64.28"
Pattern : "^value\s{2,}\d+(\.\d+)?"

Both without quotes, of course.

$input = InputBox("input", "")
$pattern = InputBox("pattern", "")


    $array = StringRegExp($input, $pattern, 1)

    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next

Now, that pattern should return "value 795.45" however, it only returns ".45". Is this intended, or an alternate method of getting the string I want? I've had this issue every time I've used optional groups. It's the only thing that gets returned.

Edited by gamepin126

Share this post


Link to post
Share on other sites



I think you messed up your pattern (posted the wrong one), because a string starting with 'value' will never match '^white'.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I think you messed up your pattern (posted the wrong one), because a string starting with 'value' will never match '^white'.

Ah, I guess I did. But that's not the issue. I'll fix that in the post but those are just test words. Edited by gamepin126

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Yeah, I figured you would have noticed it. But your problem is very simple to solve: some RegEx software (like, I assume, RegexBuddy) default to returning the whole match in the first element of the returned array.

AutoIT doesn't do that: you have to instruct it to do so by using flag 2.

"2 - Return array of matches including the full match (Perl / PHP style)."

Edited by danielkza

Share this post


Link to post
Share on other sites

Yeah, I figured you would have noticed it. But your problem is very simple to solve: some RegEx software (like, I assume, RegexBuddy) default to returning the whole match in the first element of the returned array.

AutoIT doesn't do that: you have to instruct it to do so by using flag 2.

"2 - Return array of matches including the full match (Perl / PHP style)."

That did it, thanks. And thanks for the quick responses. However, it's also returning the correct match, and ".45" in the next element which is a little annoying, but workable.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Just make the last group non-capturing:

"^value\s{2,}\d+(?:\.\d+)?"

Edited by danielkza

Share this post


Link to post
Share on other sites

Awesome, I'm still new to regex.

Thanks again!

Share this post


Link to post
Share on other sites

RegExBuddy is not great for use with the PCRE reg ex engine. I've actually stopped using it entirely.


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

#9 ·  Posted (edited)

RegExBuddy is not great for use with the PCRE reg ex engine. I've actually stopped using it entirely.

Hmm, so then are you using Expresso? It's the only other editor I know of. That's also really unfortunate to hear. I use EditPad Pro as my main text editor, it's so handy! Edited by gamepin126

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