Jump to content

[Regexp help] capture a string (and possibly parentheses)


nikink
 Share

Recommended Posts

Hi all, I'm having trouble with a bit of regexp.

I have:

$string1 = 'this is a (test string)'
$string2 = 'Another string'

$aNewString1 = StringRegExp($string1, '([.*])([\(].*[\)])', 1)
$aNewString2 = StringRegExp($string2, '([.*])([\(].*[\)])', 1)

which isn't working. I'm a bit stumped.

What I'm aiming for is to generate an array of matches:

$aNewString1[0] = 'this is a'

$aNewString1[1] = 'test string' (or '(test string)'; either format is ok)

$aNewString2[0] = 'Another string'

Any regexp whizzes out there who are willing to help? Please?

Link to comment
Share on other sites

piece of cake :)

"\((.*?)\)|[^\(;\)]+" using mode 3.

You just need to remember the "|" for or. Thats the way to do it. I am working on a better one to deal with nested brackets at the moment in a different project, its a bit messy at the moment, but if you want it i'll see what I can do.

Mat

Edit: Now returns it in your preferred way (no brackets). What do you want to do with StringSplit manadar?

Edited by Mat
Link to comment
Share on other sites

What do you want to do with StringSplit manadar?

There's no need to use the heavy reg exp library. A simple StringSplit will provide the same results.

This is only valid assuming that he is looking to get the right results, and not looking to learn regular expressions.

#include <Array.au3>

$string1 = 'this is a (test string)'
$string2 = 'Another string'

$aString1 = StringSplit($string1, "()")
$aString2 = StringSplit($string2, "()")

_ArrayDisplay($aString1)
_ArrayDisplay($aString2)
Link to comment
Share on other sites

#include <Array.au3>
$string1 = 'this is a (test string)'
$string2 = 'Another string'

$aNewString1 = StringRegExp($string1, '\(.*?\)|[^\(\)]+', 3)
ConsoleWrite(_ArrayToString($aNewString1, @CRLF) & @CRLF)
$aNewString2 = StringRegExp($string2, '\(.*?\)|[^\(\)]+', 3)
ConsoleWrite('! - - -' & @CRLF)
ConsoleWrite(_ArrayToString($aNewString2, @CRLF) & @CRLF)

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

Link to comment
Share on other sites

Thankyou thankyou thankyou all.

I don't understand the regexp used by Mat or Xenobiologist, but I would like to.

Manadar's suggestion makes a great deal of sense - I've been playing with regexp just enough such that all string manipulation problems have started looking like they need regexp to solve! Presumably the StringSplit wuld be faster. I didn't realise you could split by '()' and capture the string between the (). Unfortunate side effect of that is an empty cell trailing though.

Link to comment
Share on other sites

That is an unfortunate side effect, indeed. C#'s StringSplit has an option to clear any empty fields. Perhaps such a function would be suitable for AutoIt as well, in the form of a user defined function.

The second StringSplit parameter, indicates a number of characters which StringSplit uses to indicate a new array.

This example may improve your knowledge of how StringSplit can be used:

#include<Array.au3>
$a = StringSplit("a,b.c(d)f;h", ",.();")
_ArrayDisplay($a)
Link to comment
Share on other sites

All fine, or do you still need an explanantion of the used pattern?

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

Link to comment
Share on other sites

  • Moderators

There's no need to use the heavy reg exp library. A simple StringSplit will provide the same results.

This is only valid assuming that he is looking to get the right results, and not looking to learn regular expressions.

#include <Array.au3>

$string1 = 'this is a (test string)'
$string2 = 'Another string'

$aString1 = StringSplit($string1, "()")
$aString2 = StringSplit($string2, "()")

_ArrayDisplay($aString1)
_ArrayDisplay($aString2)

This is also valid only if someone knows exactly how many parenthesis are in the string they are looking for, or can assume through looping through the array that a blank means end of find and index before is the match.

Regexp is most definately the correct procedure here to prevent having to do multiple lines of code to find the answers that one line of code will do.

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.

Link to comment
Share on other sites

This is also valid only if someone knows exactly how many parenthesis are in the string they are looking for, or can assume through looping through the array that a blank means end of find and index before is the match.

Regexp is most definately the correct procedure here to prevent having to do multiple lines of code to find the answers that one line of code will do.

This is completely false. Demonstrate your principle with some code, back your story up.
Link to comment
Share on other sites

  • Moderators

This is completely false. Demonstrate your principle with some code, back your story up.

I jumped the gun, I didn't read the user actually wanting the other parts of the string, and in fact, the only element that is empty is the last.

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.

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