Jump to content

(unsolvable?) problem with StringRegExp flag 3, trying to match repeated capture groups


Go to solution Solved by SmOke_N,

Recommended Posts

$text = "abcdef header bad 32 bad 24 good 76 bad 43 bad 21 good 98 bad 
51 good 35"

Using a regular expression (because real world use is much more complex than this example) I want to extract an array of all the numbers just after a "good" word, only if the word "header" is found before the first match.

I tried this (failing) solution:

$array_of_results = StringRegExp($text,"header.*(?:good ([0-9]+) .*)+", 3)

but it only extracts the last match.

Then I tested this other (uncompleted therefore failing) solution:

$array_of_results = StringRegExp($text,"(?:good ([0-9]+) .*)+", 3)

but as you see it doesn't verify the "header"...

Does a working solution exist?

Link to comment
Share on other sites

$array_of_results = StringRegExp($text,"(?:good ([0-9]+))", 3)
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

  • Moderators
  • Solution

"(?i)(?<=header).*?(?:goods*(d+).*?)+"

Edit:

I think I misunderstood you, I thought you only wanted the first good after the header.

I think mikell is on the right track, the code is close, but doesn't make sure you're only pulling if header exists.

$s_tmptext = StringRegExpReplace($text, '^(.*?)(header.*?)\z', "$2")
; with extended, we know we are only pulling from good after a header match
If @extended Then
    $array_of_results = StringRegExp($s_tmptext, '(?:good\s*(\d+))', 3)
    _ArrayDisplay($array_of_results)
EndIf
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Considering how the regex engine works, the right way should NOT be using a positive lookbehind but a negative lookahead

And this one works even if 'header' doesn't exist  :)

#Include <Array.au3>

$text = "abcdef good 01 bad 01 good 02 header bad 32 bad 24 bad 43 good 76 bad 21 good 98 bad 51 good 35"

$res = StringRegExp($text, "(?:good\s*(\d+)(?!.*?header))", 3)

 _ArrayDisplay($res)
Link to comment
Share on other sites

  • Moderators

 

Considering how the regex engine works, the right way should NOT be using a positive lookbehind but a negative lookahead

And this one works even if 'header' doesn't exist  :)

Yeah, well... I blame it on a 2 year break :P.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 2 weeks later...

 

"(?i)(?<=header).*?(?:goods*(d+).*?)+"

Edit:

I think I misunderstood you, I thought you only wanted the first good after the header.

I think mikell is on the right track, the code is close, but doesn't make sure you're only pulling if header exists.

$s_tmptext = StringRegExpReplace($text, '^(.*?)(header.*?)\z', "$2")
; with extended, we know we are only pulling from good after a header match
If @extended Then
    $array_of_results = StringRegExp($s_tmptext, '(?:good\s*(\d+))', 3)
    _ArrayDisplay($array_of_results)
EndIf

 

Thanks Smoke, your solution is the only one that works.

I just modified the parameters of the first StringRegExpReplace with these:

'A.*?(header.*)z', "$1")

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...