Aceguy Posted February 17, 2010 Posted February 17, 2010 (edited) hi guys... stuck on this regex....i want to validate this line of text with regex....Please be advised that the above invoice has been dispatched to the current addressso i want to validate it by has|have and the word dispatched and it MUST contain BOTH instances.... has dispatched | have dispatched...i have tried(has|have)dispatchedbut it only validates dispatched, and ignnores has|have....so basically the dialog must contain (has or have) and must also contain (dispatched) Edited February 17, 2010 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock
notsure Posted February 17, 2010 Posted February 17, 2010 Im not sure what you're trying to do. Please rephrase the problem... You want "Dispatched" how? Something like this:Please be advised that the above invoice has been has dispatched|have dispatched to the current addressor what? Please be more clear about what you're trying to do.
Aceguy Posted February 17, 2010 Author Posted February 17, 2010 the dialog must contain (has or have) and must also contain (dispatched) [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock
notsure Posted February 17, 2010 Posted February 17, 2010 Its better to validate the line with stringinstr then i think. I dont know if i get it still,... but you want to be sure HAS OR HAVE AND DISPATCHED are in the string?
Aceguy Posted February 17, 2010 Author Posted February 17, 2010 (edited) must be regex...but you want to be sure HAS OR HAVE AND DISPATCHED are in the string?that is correct Edited February 17, 2010 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock
notsure Posted February 17, 2010 Posted February 17, 2010 #include <Constants.au3> local $string="Please be advised that the above invoice have been dispatched to the current address" if StringRegExp($string, "(?i:.*has|have.*dispatched.*)") = 1 Then msgbox(1,"","got it.") EndIf
99ojo Posted February 17, 2010 Posted February 17, 2010 (edited) Hi, notsure points the way, but it isn't the goal you want: $string0 = "Match:Please be advised that the above invoice has been dispatched to the current address" $string1 = "Match:Please be advised that the above invoice have been dispatched to the current address" $string2 = "No Match:Please be advised that the above invoice have been dispatchd to the current address" $string3 = "No Match:Please be advised that the above invoice has been dispatchd to the current address" $string4 = "No Match:Please be advised that the above invoice hav been dispatched to the current address" For $i = 0 To 4 $match = StringRegExp (Eval ("string" & $i), "(?i:.*has|have)(?i:.*dispatched.*)") Consolewrite ($match & " " & Eval ("string" & $i) & @CRLF) Next ;-)) Stefan Edited February 17, 2010 by 99ojo
notsure Posted February 17, 2010 Posted February 17, 2010 Then i still don't get the goal.. He wants to be sure have/has is in the string AND dispatched is in the string. Well, in my code they are and then enters the If/Then statement. I'm confused ;(
99ojo Posted February 17, 2010 Posted February 17, 2010 (edited) Then i still don't get the goal.. He wants to be sure have/has is in the string AND dispatched is in the string. Well, in my code they are and then enters the If/Then statement. I'm confused ;(Hi,Your string matches your pattern, thats right, but fits only some combinations of (have or has) And (dispatched). See my test strings.Just take my code and put your pattern in. You will see, that $match is 1 on $string3, which isn't the goal.;-))Stefan Edited February 17, 2010 by 99ojo
notsure Posted February 17, 2010 Posted February 17, 2010 Hi,Your string matches your pattern, thats right, but fits only some combinations of (have or has) And (dispatched). See my test strings.Just take my code and put your pattern in. You will see, that $match is 1 on $string3, which isn't the goal.;-))StefanAh ok... thanks for the explanation, you're right.
Aceguy Posted February 17, 2010 Author Posted February 17, 2010 (edited) thanks guys for your input, both have been greatfullly appreciated.... thanks again.... i never have got the hang of regex. Edited February 17, 2010 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock
dani Posted February 18, 2010 Posted February 18, 2010 @Aceguy It's really not that difficult. You were very close but have to realize the input string ("Please be advised that the above invoice has been dispatched to the current address") has to match the *entire* regexp. In your case, that only was '(has|have)dispatched'. An input sentence like "hasdispatched" or "havedispatched" would have matched that. However, wildcards are needed to match the *other* characters. That's why you need .* to pad it here and there (. matches any character, * = 0 or more times). The ?i makes it case insensitive.
99ojo Posted February 18, 2010 Posted February 18, 2010 (edited) @AceguyIt's really not that difficult. You were very close but have to realize the input string ("Please be advised that the above invoice has been dispatched to the current address") has to match the *entire* regexp. In your case, that only was '(has|have)dispatched'. An input sentence like "hasdispatched" or "havedispatched" would have matched that. However, wildcards are needed to match the *other* characters. That's why you need .* to pad it here and there (. matches any character, * = 0 or more times). The ?i makes it case insensitive.Hi,thanks for further excurse to regexp. I started only with the suggestion from @notsure and did the try and error method and find as result the solution.But i couldn't explain, why @notsures code doesn't match the goal.;-))Stefan Edited February 18, 2010 by 99ojo
jchd Posted February 18, 2010 Posted February 18, 2010 But i couldn't explain, why @notsures code doesn't match the goal.Let's look at it. The pattern was "(has|have)dispatched". To go thru regexps, put them in plain language, e.g. english (I can't write german for such things).The pattern will match an input containing one of the sequences "has" or "have" immediately followed by the sequence "dispatched".You now "see" that you need to get rid of the "immediately" part. You can do so by inserting the pattern ".+" meaning that "has" or "have" have to be separated by at least one character for the whole pattern to match. You then end up with "(has|have).+dispatched" which works.Even if that last patterns works, I find it a bit imprecise. At least, the words we're after should be treated like words, this way substrings in "shave" or "phasing out" won't produce false positives if ever the phrasing changes: "(?i)\bha(s|ve)\b.*\bdispatched\b" This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
99ojo Posted February 19, 2010 Posted February 19, 2010 Let's look at it. The pattern was "(has|have)dispatched". To go thru regexps, put them in plain language, e.g. english (I can't write german for such things).The pattern will match an input containing one of the sequences "has" or "have" immediately followed by the sequence "dispatched".You now "see" that you need to get rid of the "immediately" part. You can do so by inserting the pattern ".+" meaning that "has" or "have" have to be separated by at least one character for the whole pattern to match. You then end up with "(has|have).+dispatched" which works.Even if that last patterns works, I find it a bit imprecise. At least, the words we're after should be treated like words, this way substrings in "shave" or "phasing out" won't produce false positives if ever the phrasing changes: "(?i)\bha(s|ve)\b.*\bdispatched\b"Hi,good explanation. Makes it more and more clear.Thanks a lot.;-))Stefan
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