Imbuter2000 Posted November 24, 2014 Share Posted November 24, 2014 $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 More sharing options...
MikahS Posted November 24, 2014 Share Posted November 24, 2014 (edited) $array_of_results = StringRegExp($text,"(?:good ([0-9]+))", 3) Edited November 24, 2014 by MikahS Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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 More sharing options...
mikell Posted November 24, 2014 Share Posted November 24, 2014 Don't over complicate $res = StringRegExp(StringRegExpReplace($text, '.*?header', ""), '(?:good\s*(\d+))', 3) Link to comment Share on other sites More sharing options...
Moderators Solution SmOke_N Posted November 24, 2014 Moderators Solution Share Posted November 24, 2014 (edited) "(?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 November 24, 2014 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 More sharing options...
mikell Posted November 24, 2014 Share Posted November 24, 2014 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 More sharing options...
Moderators SmOke_N Posted November 24, 2014 Moderators Share Posted November 24, 2014 (edited) 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 . Edited November 24, 2014 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 More sharing options...
Imbuter2000 Posted December 3, 2014 Author Share Posted December 3, 2014 "(?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 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