Sign in to follow this  
Followers 0
andybiochem

Regular Expressions [solved]

16 posts in this topic

#1 ·  Posted (edited)

Hi!

I'm trying to get my head around regular expressions, but it's doing my nut!

say I wanted to match the following list of strings:

12,25.5,ABC

1,97,Nam3

3,100,Z

but NOT:

12,25.5,ABC,zzz

(notice the ",zzz" bit at the end)

how do I do this with stringregexp?

So far I've got:

"([0-9]{1,}[,][0-9]{1,}[.]?([0-9]{1,})?[,][[:alnum:]]{1,})"

but this sees a match and returns it. I want the inputted string to match exactly the reg exp given....if there's more data in the string than specified in the reg exp I want it to fail.

Thanks in advance for any help!!

Edited by andybiochem

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites



Hi,

you need to think of a regular method of your strings. Match all, but not if they end with zzz? Is that the rule?

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi,

you need to think of a regular method of your strings. Match all, but not if they end with zzz? Is that the rule?

Mega

Not just zzz, anything at all after the 3rd item.

the way I'm thinking of my string to match is:

[any integer][comma][any number including decimal places][comma][any word/number/both]

if the whole string doesn't match this format then I want regexp to return = 0

but because it sees that format above within "12,25.5,ABC,zzz" it returns success (=1).

I want it to look at the whole string, not just pick and choose where it finds matches mid-string.

thanks for the reply tho!

Edited by andybiochem

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

Hi,

try this:

\d+,\d+[\.]*\d*,\w*(?=\s)

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

"(?s)(\d+,\d+\.*\d*,\w+)(?:\z|\s)"


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Thank you both!

SmOke_N yours seems to work for every example i've tried, so I'll go with that.

Cheers!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

I eventually found some examples that wouldn't work with Smoke's reg exp :P

So this is how I managed to get it to work:

$string = "3,25.0,Text here" ;this should match
;$string = "zzz,123,32.33,Text Here" ;this shouldn't match
;$string = "123,32.33,Text Here...." ;this shouldn't match

;----- check string for reg exp -----
$aMatches = StringRegExp($string,"(?:\D*)(\d+,\d+.?\d?,[\w ]*)(?:\W*)",1)

;----- if no matches found... -----
If @error = 1 Then 
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- if match found not the same as original string... -----
If $aMatches[0] <> $string Then
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- other-wise good match -----
MsgBox(0,"","String format good!!!")

The important bit was the second If>EndIf section. If the 'matched' string wasn't identical to the original string, then the StringRegExp has cherry-picked the match out of a different, longer, string....thus for the purpose I needed this was a false-positive. I want to know if the WHOLE string matches the reg exp.

Maybe I've missed something obvious, but this is the only way I could find to get it to work.

Just thought I'd share. :P


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yours:

(?:\D*)(\d+,\d+.?\d?,[\w ]*)(?:\W*)

Smoke_N's:

(?s)(\d+,\d+\.*\d*,\w+)(?:\z|\s)

These don't match. You said you were going to use Smoke_N's. Does it work with his?

You have a workaround. I'm just curious.

Edited by c0deWorm

My UDFs: ExitCodes

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I eventually found some examples that wouldn't work with Smoke's reg exp :P

So this is how I managed to get it to work:

$string = "3,25.0,Text here" ;this should match
;$string = "zzz,123,32.33,Text Here" ;this shouldn't match
;$string = "123,32.33,Text Here...." ;this shouldn't match

;----- check string for reg exp -----
$aMatches = StringRegExp($string,"(?:\D*)(\d+,\d+.?\d?,[\w ]*)(?:\W*)",1)

;----- if no matches found... -----
If @error = 1 Then 
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- if match found not the same as original string... -----
If $aMatches[0] <> $string Then
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- other-wise good match -----
MsgBox(0,"","String format good!!!")

The important bit was the second If>EndIf section. If the 'matched' string wasn't identical to the original string, then the StringRegExp has cherry-picked the match out of a different, longer, string....thus for the purpose I needed this was a false-positive. I want to know if the WHOLE string matches the reg exp.

Maybe I've missed something obvious, but this is the only way I could find to get it to work.

Just thought I'd share. :P

You changed the situation:

Text<space>Here was not in your specs.

Edit:

You know, actually it would be MUCH better if you just posted the actual file you were trying to manipulate rather than trying to make scenarios. You leaving the slightest thing out changes an expression completely.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You changed the situation:

Text<space>Here was not in your specs.

Yeah, my fault. :P

should have said:

[any integer][comma][any number including decimal places][comma][any words/numbers/Chr(32)s]

Really appreciated the help though, really made me think about reg exp in a different way.


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

You know, actually it would be MUCH better if you just posted the actual file you were trying to manipulate rather than trying to make scenarios. You leaving the slightest thing out changes an expression completely.

It wasn't actually a file I was working on. The scenario was in fact one I had made....this reg exp was a way of checking a user's input was in the correct format (a format I had devised) and that they hadn't just typed in junk.


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Lol, found more faults. Now I'm working with:

"(?:\D*)(\d+,\d+(.\d+)*,[\w ]*)(?:\W*)"

:P

@SmOke_N

Is there a less draw-out way of doing this?:

$string = "3,25.0,Text here" ;this should match
;$string = "zzz,123,32.33,Text Here" ;this shouldn't match
;$string = "123,32.33,Text Here...." ;this shouldn't match

;----- check string for reg exp -----
$aMatches = StringRegExp($string,"(?:\D*)(\d+,\d+(.\d+)*,[\w ]*)(?:\W*)",1)

;----- if no matches found... -----
If @error = 1 Then
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- if match found not the same as original string... -----
If $aMatches[0] <> $string Then
    MsgBox(0,"","String format not correct")
    Exit
EndIf

;----- other-wise good match -----
MsgBox(0,"","String format good!!!")

...ignore the msgboxes they're just there to check correct returns.

Edited by andybiochem

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

I haven't analyzed everything.. but have you tried using ^ and $ to match the start and end of a string?

Just going off your first example of "[any integer][comma][any number including decimal places][comma][any word/number/both]" try this?

^\d+,\d+(\.\d+)?,[\w ]+$

Share this post


Link to post
Share on other sites

I haven't analyzed everything.. but have you tried using ^ and $ to match the start and end of a string?

Just going off your first example of "[any integer][comma][any number including decimal places][comma][any word/number/both]" try this?

YES! That's it!! Thanks you!

^ and $ ...where on earth did they come from? lol!

Can this be added to the help file? Their use is mentioned here:

http://en.wikipedia.org/wiki/Regular_expression

Thanks for everyone's help! much appreciated.


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

I could have sworn they were in the documentation as they're pretty standard, but when I checked sure enough they aren't there. They are mentioned indirectly with the (?m) flag. There is a lot of stuff that RegEx is capable of that's not mentioned in the AutoIt doc's though. There's a link near the top of that page which links here though, which is a more detailed description of the RegEx syntax.

Share this post


Link to post
Share on other sites

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