Jump to content
Sign in to follow this  
Hawkwing

stringregexp string has parentheses

Recommended Posts

Hawkwing

I think I understand the basics of stringregexp, thanks to neogia's guide, but what if the test string has parentheses?

So if the test string is "test(#)", and I want to have a pattern like "test([1-9])" how do I do that?


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites
BrettF

Hi There!

This is what I came up with first, but we have a very obvious problem. It only returns what is between the parentheses.

Dim $test[6]
$test[0] = 5
$test[1] = "Test (100)"
$test[2] = "Test (200)"
$test[3] = "Test (300)"
$test[4] = "Test (a400)"
$test[5] = "Test (500)"

For $x = 1 To $test[0]
    $array = StringRegExp($test[$x], "Test \((.*?)\)", 3)

    For $i = 0 To UBound($array) - 1
        MsgBox(0, "RegExp Test with Option 2 - " & $i, $array[$i])
    Next
Next

So I refined the pattern to only return digits:

Dim $test[6]
$test[0] = 5
$test[1] = "Test (100)"
$test[2] = "Test (200)"
$test[3] = "Test (300)"
$test[4] = "Test (a400)"
$test[5] = "Test (500)"

For $x = 1 To $test[0]
    $array = StringRegExp($test[$x], "Test \((\d.*?)\)", 3)

    For $i = 0 To UBound($array) - 1
        MsgBox(0, "RegExp Test with Option 2 - " & $i, $array[$i])
    Next
Next

I'm sure someone will come along with a better regular expression, but I hope this helps somewhat...

Cheers,

Brett

Share this post


Link to post
Share on other sites
Hawkwing

Thanks! Just went to the helpfile and found out what \ was for, should have done that before I posted :D


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.