Sign in to follow this  
Followers 0
Aceguy

regex again...

15 posts in this topic

#1 ·  Posted (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 address

so 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)dispatched

but it only validates dispatched, and ignnores has|have....

so basically the dialog must contain (has or have) and must also contain (dispatched)

Edited by Aceguy

Share this post


Link to post
Share on other sites



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 address

or what? Please be more clear about what you're trying to do.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by 99ojo

Share this post


Link to post
Share on other sites

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 ;(

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by 99ojo

Share this post


Link to post
Share on other sites

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

Ah ok... thanks for the explanation, you're right.

Share this post


Link to post
Share on other sites

@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.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

@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.

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 by 99ojo

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

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

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