sugi Posted November 25, 2005 Posted November 25, 2005 Hello,I've got a problem with a regular expression. I have a two lines: "foobar skill" and "something else skill". Now i want to extract this "foobar" or "something else".So i created this regular expression: "(\w+( \w+)?) skill"In my reading of this RE this means:match the first wordmaybe have a space followed by another worda spacethe word "skill"According to the regex coach this expression should work, but if the text has three words in total it does not work in AutoIt as the following code shows:$text="foobar skill" $regexp='(\w+( \w+)?) skill' $x = Number(StringRegExp($text, $regexp, 0)) MsgBox(64, 'RegExp?', $x) $text="something else skill" $regexp='(\w+( \w+)?) skill' $x = Number(StringRegExp($text, $regexp, 0)) MsgBox(64, 'RegExp?', $x)But if I change the "?" to a "*" it works. Which is strange I think. After all the "?" should mean "zero or one" (and in case of the two word line it should mean zero) and the "*" means "zero or more" (and in case of the two word line it means zero). So both should be give the same result for the lines I used to test them.I cant figure out what's wrong in my expression. Do you have any idea?
LxP Posted November 26, 2005 Posted November 26, 2005 I'm not entirely sure of what you're trying to do but perhaps this will help:StringRegExp('something else skill', '(.+?)\w+skill')(Match anything that occurs before some whitespace and the word 'skill'.)
Francis Lennert (France) Posted November 26, 2005 Posted November 26, 2005 Hello All, Maybe that will help. If you want to extract from strings, I think that you must take a look at the last Flag (3) from the StringRegExp command. That will create an array with values which are simple to use. The ( and ) will help you to create Blocks which will be values in the Array. HTH, Francis Dim $i Dim $Text[2] Dim $Tokens $text[0]="foobar skill" $text[1]="something else skill" $regexp="^(.*)( skill)" for $i=0 to 1 $tokens = StringRegExp($text[$i], $regexp, 3) MsgBox(64, 'RegExp?', $tokens[0]) next
sugi Posted November 26, 2005 Author Posted November 26, 2005 I know about the StringRegExp flag and that 0 only tells you if there's a match or not. I'M only using 0 because I wanted to validate if it matches at all because I had strange results. Simply using ".*" doesnt help me as the actual line is a lot longer. I shortened it to the actual problem I have. so neither ".*" or ".+?" is usable for me. I need to match "a word", possibly followed by "a space and a second word" and always followed by " skill".
LxP Posted November 26, 2005 Posted November 26, 2005 How about this then: StringRegExp('something else skill', '(\w+\s+){1,2}skill')
sugi Posted November 26, 2005 Author Posted November 26, 2005 StringRegExp('something else skill', '(\w+\s+){1,2}skill')That works if I add a StringStripWS as I dont want the last space. But I'm still trying to figure out why my regex doesnt work. After all at least one program accepts them, so it cannot only be a wrong regex.
sugi Posted November 26, 2005 Author Posted November 26, 2005 Funny thing: I just tried this code:$text="Someone's fishing skill rises 0.2 points.1" $regexp='\w+.s ((\w+\s+){1,2})skill rises (\d+.\d+) points\.1' $x = Number(StringRegExp($text, $regexp, 0)) MsgBox(64, 'RegExp?', $x) It tells me it doesnt match. But if I use$regexp='\w+.s (\w+\s+){1,2}skill rises (\d+.\d+) points\.1'It works. It looks like AutoIt does not support nested groups.
Francis Lennert (France) Posted November 26, 2005 Posted November 26, 2005 Dim $i Dim $Text[4] Dim $Tokens $text[0]="foobar skill"; Good $text[1]="something super else skill"; not good $text[2]="something else skill"; good $text[3]="something else skill"; good $regexp="^(\w+\s{1,2}){1,2}skill" for $i=0 to 3 $tokens = StringRegExp($text[$i], $regexp, 0) MsgBox(64, 'RegExp?', $tokens) next ??????????????????? seems to work
sugi Posted November 26, 2005 Author Posted November 26, 2005 (edited) $regexp="^(\w+\s{1,2}){1,2}skill"That regex has a different meaning: A word, followed by one or two spaces. Your text[3] should NOT work. I'm talking about groups within other groups because I dont want to use postprocessing at all when the regex is able to give me what i want. Edited November 26, 2005 by sugi
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