Sign in to follow this  
Followers 0
ahaning

Regex pattern to match regex patterns

13 posts in this topic

Does anyone know of a regex pattern that can be used to match (and then replace) regex patterns in a string?

For instance, if I had a pattern FOOBAR[0-9][0-9][0-9] which would normally match FOOBAR123 and FOOBAR321, I'm looking for a pattern that would match the [0-9][0-9][0-9] part. The same would go for a pattern FOOBAR.* or FOO\wBAR or whatever. If I were to replace the matched part in any of those examples with a blank string ("") I would get back just FOOBAR.

Any ideas?

Share this post


Link to post
Share on other sites

Haven't you watched Dogma? Doing this will unmake mankind!

Share this post


Link to post
Share on other sites

Does anyone know of a regex pattern that can be used to match (and then replace) regex patterns in a string?

For instance, if I had a pattern FOOBAR[0-9][0-9][0-9] which would normally match FOOBAR123 and FOOBAR321, I'm looking for a pattern that would match the [0-9][0-9][0-9] part. The same would go for a pattern FOOBAR.* or FOO\wBAR or whatever. If I were to replace the matched part in any of those examples with a blank string ("") I would get back just FOOBAR.

Any ideas?

I wrote a RegExp that creates and edits other RegExp's back in April of 2004, and finished debugging it on August 4th. Didn't end well. The RegExp began to learn at an exponential rate. It became self-aware at 2:14 a.m. Eastern time, August 29th. In a panic, I tried to pull the plug, but it couldn't be shut down. Looking for a powerful location from which to gain control of the net, it created an online persona for itself on February 20th, 2005.

The rest as they say... is history.

<_<


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

Share this post


Link to post
Share on other sites

I wrote a RegExp that creates and edits other RegExp's back in April of 2004, and finished debugging it on August 4th. Didn't end well. The RegExp began to learn at an exponential rate. It became self-aware at 2:14 a.m. Eastern time, August 29th. In a panic, I tried to pull the plug, but it couldn't be shut down. Looking for a powerful location from which to gain control of the net, it created an online persona for itself on February 20th, 2005.

The rest as they say... is history.

<_<

:) --- Said WTF when I clicked on the link :P

I'm on my way into work, I'll have a look again at this question when I get there.


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.

Share this post


Link to post
Share on other sites

Not quite sure I'm understanding the exact request... but I'll take a stab at what I think you're asking anyway:

$sString = "I am foobar321."
$sText = StringRegExpReplace($sString, "(?s)(?i)[^foobar]\d+", "")
MsgBox(0, 0, $sText)
?


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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Smoke I'm pretty sure he wants to match the pattern itself.

So I would think you would look for 3 sets of brackets.

"FOOBAR[0-9][0-9][0-9]"

Edited by weaponx

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Smoke I'm pretty sure he wants to match the pattern itself.

So I would think you would look for 3 sets of brackets.

"FOOBAR[0-9][0-9][0-9]"

I had it wrong with what I was thinking anyway
$sString = "I am foobar321 abc134."
$sText = StringRegExpReplace($sString, "(?s)(?i)(foobar)\d{3}", "\1")
MsgBox(0, 0, $sText)oÝ÷ Ù8^ÓÝíg¬Éè¶wz˯+a¢è!²0¦è½áÑÞºÇÍêè¶îö+0",j'b·¬mç²Æ«Ê'áz+-¯(§Ú¢Ø^éz»"§®Ö¢Ç(¥ë(ëax%G­æÞ0¢é]ÂäjÌV®¶­sbb33c·57G&ærÒgV÷C´Òföö&#c&33BâgV÷C°¢b33c·5FWBÒ7G&æu&VtW&WÆ6Rb33c·57G&ærÂgV÷C²÷2öföö&"³bÓ׳7ÒgV÷C²ÂgV÷C²b3#³gV÷C²¤×6t&÷ÂÂb33c·5FWB
Edited by SmOke_N

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.

Share this post


Link to post
Share on other sites

Smoke I'm pretty sure he wants to match the pattern itself.

Right.

So I would think you would look for 3 sets of brackets.

"FOOBAR[0-9][0-9][0-9]"

Not exactly.

Rather, n sets of brackets. Basically, if regex's had "tags" I'd want to remove the tags and anything inside of them. Unfortunately, brackets aren't the only indicators of a regex pattern.

I understand that it's probably impossible to do with AutoIt1 but thought I'd ask in case it isn't.

[1] If the AutoIt community is anything like the Linux community, the solutions will come pouring in now. ;)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Right.

Not exactly.

Rather, n sets of brackets. Basically, if regex's had "tags" I'd want to remove the tags and anything inside of them. Unfortunately, brackets aren't the only indicators of a regex pattern.

I understand that it's probably impossible to do with AutoIt1 but thought I'd ask in case it isn't.

[1] If the AutoIt community is anything like the Linux community, the solutions will come pouring in now. <_<

Wait... Your trying to find a literal regular expression string?

Care to elaborate (A Lot)?

Some examples of what you would be searching, and what the expected output desired, would be great as well.

Edit:

?

$sString = "I am FOOBAR[0-9][0-9][0-9] abc134."
$sPre = "foo\w*bar.*?"
$sText = StringRegExpReplace($sString, "(?s)(?i)(" & $sPre & ")(\[[\d|-]+\])*", "\1")
MsgBox(0, @extended, $sText)
?? Edited by SmOke_N

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.

Share this post


Link to post
Share on other sites

Wait... Your trying to find a literal regular expression string?

Care to elaborate (A Lot)?

Some examples of what you would be searching, and what the expected output desired, would be great as well.

I guess you got it now. :(

I'm trying to find a list of files based on a regex. So:

FOOBAR001

FOOBAR002

FOOBAR003

FOOBAR004

FOOBAR005

FOOBAR006

or

FOO001BAR

FOO002BAR

FOO003BAR

FOO004BAR

FOO005BAR

FOO006BAR

So my patterns would be FOOBAR[0-9][0-9][0-9] or FOO[0-9][0-9][0-9]BAR, respectively.

When I get the list of files, I find one and upload it to a server, outputting the result to a log file whose name is based on the regex - but I can't name the file with some of the regex characters in it (slashes or asterisks, for example). So I was hoping to be able to use FOOBAR.log for either of the above cases. At first, I wrote a simple UDF to replace all non-alpha characters with some other character (ex: ""). This works until you have:

FOO1234[0-9][0-9][0-9]BAR

...and you get FOOBAR out of it instead of FOO1234BAR.

Does this make any more sense?

Share this post


Link to post
Share on other sites

I've gone crosseyed.

Share this post


Link to post
Share on other sites

If the edit I made above doesn't work for you, then unfortunately, I still have no idea what you are talking about, or (obviously if I don't understand) how to accomplish what it is you want.


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.

Share this post


Link to post
Share on other sites

Thanks. I'll try it out and if it doesn't work, I'll just strip the non-alphanumeric characters.

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