Jump to content

RegExp Hmm Question


czardas
 Share

Recommended Posts

I have looked at the help file several times and tried to find something on the forum, but I decided it would be quicker to ask.

I have never used regular expressions and I think it's just what I need. I wish to test if a string qualifies as a word. Why not use StringIsAlpha you might ask? Well that doesn't help with hyphenated words, nor does it help to test for apostrophes. The expression would have to indicate that either a hyphen or an apostrophe was found somewhere within a non-case sensitive alphabetical string. So where's my code? Well hmm? I haven't got a clue where to begin. :)

Could someone please give me an example of this.

$returnValue = "God only knows?"

Dim $array[10]
$array[0] = 9
$array[1] = "ab-c" ; Should return TRUE because 1 hyphen appears between sets of letters.
$array[2] = "ab'c" ; Should return TRUE because 1 apostrophe  appears between sets of letters.
$array[3] = "-abc" ; Should return FALSE because the hyphen appears first.
$array[4] = "a-b-c" ; Should return FALSE because the hyphen repeats.
$array[5] = "abc-" ; Should return FALSE because the hyphen appears at the end.
$array[6] = "'abc" ; Should return FALSE because the apostrophe appears first.
$array[7] = "abc'" ; Should return FALSE because the apostrophe appears last.
$array[8] = "a''bc" ; Should return FALSE because the apostrophe repeats.
$array[9] = "a-b'c" ; Should return FALSE because it contains both apostrophe and hyphen.

For $i = 1 To $array[0] Step 1
  ;now comes a regular expression testing if each element qualifies as a word.
  MsgBox(0, "Test Result", "I know that this is or isn't a word because of some return value" & @CRLF & $returnValue)
Next

I hope this isn't asking too much. It will save me having to write a lot of unecessary code. Thanks in advance!

Edited by czardas
Link to comment
Share on other sites

Just winging it here but the first that pops to mind is

(?i)[a-z]+[\-']?[a-z]+

So we have

For $i = 1 To $array[0]
  ;now comes a regular expression testing if each element qualifies as a word.
  MsgBox(0, "Test Result", StringRegExp($array[$i], "(?i)[a-z]+[\-']?[a-z]+"))
Next

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just winging it here but the first that pops to mind is

(?i)[a-z]+[\-']?[a-z]+

So we have

For $i = 1 To $array[0]
  ;now comes a regular expression testing if each element qualifies as a word.
  MsgBox(0, "Test Result", StringRegExp($array[$i], "(?i)[a-z]+[\-']?[a-z]+"))
Next

Thanks! That gives me something to chew on. :)

Hmm. It seems that I get a return value of 1 for all tests. That's not really working, though I appreciate it. Perhaps I can figure out why it's not working.

Edited by czardas
Link to comment
Share on other sites

Thanks! That gives me something to chew on. :)

Hmm. It seems that I get a return value of 1 for all tests. That's not really working, though I appreciate it. Perhaps I can figure out why it's not working.

Well like I said, I was winging it because I'm on the laptop with no developer software installed so lets break it down

(?i)[a-z]+[\-']?[a-z]+

(?i) - case insensitive

[a-z]+ - match alpha characters one or more times

[\-']? match hypen or comma once if it exists

[a-z]+ - match alpha characters one or more times

Now lets look at what I did wrong. I forgot to tell it to match from the beginning to the end of the string.

(?i)^[a-z]+[\-']?[a-z]+$

I've added ^ to match at the beginning of the string and $ to match at te end of the strig so it must start and end with an alpha.

Edit: Something else just occured to me.

Try changing the MsgBox line to This

For $i = 1 To $array[0]
  ;now comes a regular expression testing if each element qualifies as a word.
    If StringRegExp($array[$i], "(?i)^[a-z]+[\-']?[a-z]+$") Then
         MsgBox(0, "Test Result", "True")
    Else
        MsgBox(0, "Test Result", "False")
    EndIf
Next

EDIT 2

Again I forgot to test from beginning to end. This time in the If statement

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks! That gives me something to chew on. :)

Hmm. It seems that I get a return value of 1 for all tests. That's not really working, though I appreciate it. Perhaps I can figure out why it's not working.

It returns true because a matching pattern (any two letter with or without the punctuation between them) can be found SOMEWHERE in the string. For example, in "-abc" it can match on "ab" or "bc" or "abc", so it's true. You need to add flags to include the beginning/ending of the string.

B)

Oops. GEOSoft been there, done that already! ;)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...