Sign in to follow this  
Followers 0
syno

Regex finding exclusive figures

23 posts in this topic

Hi guys

I know this is probably quiet straight forward but I need to match specific figures. The code below matches 1 a minimum of 4 times. However I need a minimum of 4 and maximum of 4.

MsgBox(0, "SRE Example 3 Result", StringRegExp('1111', '1{4}')

Any help would be appreciated...

Thanks

Share this post


Link to post
Share on other sites



According to the helpfile:

{x, y} Repeat the previous character, set or group between x and y times, inclusive

So I'd suggest simply using

MsgBox(0, "SRE Example 3 Result", StringRegExp('1111', '1{4,4}'))

Hope this helps,

Marc


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

Hi guys

I know this is probably quiet straight forward but I need to match specific figures. The code below matches 1 a minimum of 4 times. However I need a minimum of 4 and maximum of 4.

MsgBox(0, "SRE Example 3 Result", StringRegExp('1111', '1{4}')
I think you want to match "ABC1111DEF", but not "ABC111DEF" as well as not "ABC11111DEF", right?

Try this one: (?:[^1]{1})(1{4})(?:[^1]{1})

Step by step:

(?:[^1]{1})

"(?:" opens a non capturing group

[^1] a group matching any CHAR except "1"

{1} one "non 1" will do.

(1{4}) "1" literally, exactly 4 times.

and finally again exactly one "non 1" CHAR.

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Rudi's code is good, but if the 4 1's falls at the beginning or end of a string, you'll need to add two 'OR' statements. Plus, unless you are capturing the data, you don't need the ?:, and the {1} isn't necessary since 1 instance is always implied.

Here's an example that matches anywhere in a string. Note that if you have a multi-line string, you'll need to make further adjustments (like adding (?m) at the start). Here's a modified version of Rudi's:

(^|[^1])1{4}([^1]|$)

*edit: also, the () around '1{4}' wasn't necessary (unless again you are capturing groups)

Edited by ascendant

Share this post


Link to post
Share on other sites

Hi guys

I have tried both:

(^|[^1])1{4}([^1]|$)

and

(?:[^1]{1})(1{4})(?:[^1]{1})

but I get the following error message:

Unable to parse line.:

MsgBox(0, "SRE Example 3 Result", StringRegExp('111111', (^|[^1])'1{4}'([^1]|$)))

MsgBox(0, "SRE Example 3 Result", StringRegExp('111111', (^^ ERROR

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The RegExp has to be quoted

MsgBox(0, "SRE Example 3 Result", StringRegExp('111111', "(^|[^1])'1{4}'([^1]|$)"))

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!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Unable to parse line.:

MsgBox(0, "SRE Example 3 Result", StringRegExp('111111', (^|[^1])'1{4}'([^1]|$)))

MsgBox(0, "SRE Example 3 Result", StringRegExp('111111', (^^ ERROR

Uh, why did you put a ' in there twice? And GEOSoft is correct, the string must be quoted. So use:

StringRegExp("111111","(^|[^1])1{4}([^1]|$)") ; For FALSE

StringRegExp("1111","(^|[^1])1{4}([^1]|$)") ; For TRUE

*edit: oops, put a "", in there by mistake :D

Edited by ascendant

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Fantastic, that worked. I just needed the '' or "" outside the brackets so:

MsgBox(0, "SRE Example 3 Result", StringRegExp('1111','(^|[^1])1{4}([^1]|$)'))
; TRUE

Thanks for all your help people..!

Edited by syno

Share this post


Link to post
Share on other sites

Hi guys

What if I wanted to match non digits such as the following:

:

,

/

x

-

Share this post


Link to post
Share on other sites

[:|,|/|x|-] to match the list you gave. or you could use the [:punct:] class to match any punctuation.


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!"

Share this post


Link to post
Share on other sites

[:|,|/|x|-] to match the list you gave. or you could use the [:punct:] class to match any punctuation.

I have tried:
StringRegExp('100,000','(^|[,][^1][^0])1{1}0{5},{1}([^1][^0][,]|$)'

and it returns false. What am I doing wrong?

Share this post


Link to post
Share on other sites

Hi,

can you give some examples like this

String ..... = you need ...

String ..... = you need ...

String ..... = you need ...

or instead of you need just match or no match

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

Hi,

can you give some examples like this

String ..... = you need ...

String ..... = you need ...

String ..... = you need ...

or instead of you need just match or no match

Mega

I am not entirely sure what you mean. I want to match 100,000 (with a comma). If I use the following code (without the comma) it works, ie

MsgBox(0, "SRE Example 3 Result", StringRegExp('100000','(^|[^1][^0])1{1}0{5}([^1][^0]|$)'))

How do i match 100,000 with 100,000 ?

Share this post


Link to post
Share on other sites

I need to understand what should match and what shouldn't so that I can create the correct pattern.

so :

100,000 match

100000 should not match?

123,098 mtach?

so it is always 3 digits a , and then again 3 digits?

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

You have to provide more context than that if you need to find a string inside others. With what you gave, you can do something as easily as this to grab that whole number:

MsgBox(0, "SRE Example 3 Result", StringRegExp('100000','[\d,]+'))

Share this post


Link to post
Share on other sites

I need to understand what should match and what shouldn't so that I can create the correct pattern.

so :

100,000 match

100000 should not match?

123,098 mtach?

so it is always 3 digits a , and then again 3 digits?

Mega

Hi there

Only 100,000 should match

Thanks

Share this post


Link to post
Share on other sites

\b100,000\b

Share this post


Link to post
Share on other sites

Hi,

you can also do a string compare If $var == "100,000" Then ... or RegExp with \b like above which means word boundaries.

Or if StringInStr ... which means the string is in the string not the string is 100 % the same.

There are so many possibilities to do this "easy" job

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

Hi,

you can also do a string compare If $var == "100,000" Then ... or RegExp with \b like above which means word boundaries.

Or if StringInStr ... which means the string is in the string not the string is 100 % the same.

There are so many possibilities to do this "easy" job

Mega

This seems to be a very common situation lately. "How do I use StringRegExp to do such and such?" , when there are other ways to do it. What people don't seem to realize is that RegExps, although more versatile, are generally slower than the normal string functions. When working with strings where there will be no variation in the string then you are better off using the normal functions such as StringInStr() or StringReplace().

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!"

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

StringRegExp use internal optimizations that makes this function so fast and so versatile. If it's just to search for 100,000 in 11100,000TTT___ then it's probably way better to use StringInStr but if you want just 100,000 and nothing but it, RegExp is a good choice.

Edit: By the way, StringRegExp use sort of StringInStr optimization where there is a literal text but using \d{3},\d{3} for example won't save you anything but allow you to match better than StringInStr.

Edited by Authenticity

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