Jump to content

RegExp - tuts, examples, problems


peethebee
 Share

Recommended Posts

Hi!

I just started to work with Regular Expressions. I want to use them for validation. As the help file does not give any ;) example, I would like to see some easy and some more complicated RegExps. I searched the internet already and found out, that there are several RegExp types with different syntax. That's why I would like to know which standard is used in AutoIt.

In the following script I have some problems:

I cannot specify the start of word character. The German postal code has the format "xxxxx", where every "x" stands for a number from 0 to 9. If I leave the "\<" at the beginning, also 123456 is recognized, if I pput it in, nothing is recognized as valid. Where's my fault here?

Does the or-operator ("|") need brackets or how does the interpreter know what it references to?

#include <Array.au3>
 
 Global $regexp_texts[1], $regexp[1]
 $regexp_texts[0] = 'German postal code ("Postleitzahl")'
 $regexp[0] = "[0-9]{5,5}\>"
 _ArrayAdd($regexp_texts, 'Name')
 _ArrayAdd($regexp, "[A-Z]{1}(.|[a-z]+) ([A-Z]{1}(.|[a-z]+))? [A-Z]{1}[a-z]+\>")
 
 
 Global $eval_string
 
 Opt("GUIOnEventMode", 1)
 
 $gui = GUICreate("RegExp Tester", 200, 250)
 GUISetOnEvent(-3, "Quit")
 $label_1 = GUICtrlCreateLabel("Select RegExp:", 10, 15)
 $regExpCombo = GUICtrlCreateCombo("", 10, 30, 180, 100)
 For $i = 0 to UBound($regexp_texts) - 1
     GUICtrlSetData($regExpCombo, GUICtrlRead($regExpCombo) & "|" & $regexp_texts[$i], $regexp_texts[$i])
 Next
 
 $label_2 = GUICtrlCreateLabel("Your test string:", 10, 80)
 $input = GUICtrlCreateInput("jm-software@gmx.de", 10, 95, 180)
 
 $check_button = GUICtrlCreateButton("&Check", 65, 150, 75, 25)
 GUICtrlSetOnEvent($check_button, "check")
 
 $valid_label = GUICtrlCreateLabel("valid", 85, 200, 100)
 
 GUISetState()
 
 While 1
   Sleep(1000)
 WEnd
 
 
 Func Quit()
     Exit
 EndFunc
 
 Func check()
     $eval_string = GUICtrlRead($input)
     
     For $i = 0 to UBound($regexp_texts) - 1
         If GUICtrlRead($regExpCombo) = $regexp_texts[$i] Then ExitLoop         
     Next
     $result = StringRegExp($eval_string, $regexp[$i], 0)
     
     If $result = 1 Then
         GUICtrlSetData($valid_label, "valid")
     Else
         GUICtrlSetData($valid_label, "not valid")      
     EndIf
 EndFunc

Thanks for your help,

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

Link to comment
Share on other sites

I can't really help with tutorials but does this work better for you? --

\<\d{5}\>

In my opinion what you entered should be working...

Also, the or operator (|) does require brackets otherwise it will only match one of the immediate entities that it finds to its sides.

Link to comment
Share on other sites

Hi!

Unfprtunately, your solution does not work, LxP.

Nothing is recognized as valud :-(

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

Link to comment
Share on other sites

Hi!

Unfprtunately, your solution does not work, LxP.

Nothing is recognized as valud :-(

peethebee

This excellent test program was found in a search for regexp in this forum. Only part included so you can practice using the search function here:

AutoItSetOption("MustDeclareVars", 1)

Global $Success = 1

; RegExtTest parameters:
;   1 - Test string
;   2 - Regular exp[b][/b]ression pattern
;   3 - Flag:
;       0 - pattern should not match
;       1 - pattern should match
;       2 - pattern should be invalid
;       Text string - First captured element should match text

RegExpCheck("", "^$", 1)

RegExpCheck("Test" & @CR & @
.
.
.
.
EndFunc  ;==>AbortTest
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi!

I searched for it (and searched before too) but I did not find such a thing. Could someone post the link to it please?

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

Link to comment
Share on other sites

Hi!

Thank you for your answer tuape, but I got it directly from jefhal by now.

But thank you anyway.

peethebee

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvGerman Forums: http://www.autoit.deGerman Help File: http://autoit.de/hilfe vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

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