nikink Posted October 29, 2009 Share Posted October 29, 2009 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 More sharing options...
jvanegmond Posted October 29, 2009 Share Posted October 29, 2009 StringSplit? github.com/jvanegmond Link to comment Share on other sites More sharing options...
Mat Posted October 29, 2009 Share Posted October 29, 2009 (edited) 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 October 29, 2009 by Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
jvanegmond Posted October 29, 2009 Share Posted October 29, 2009 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) github.com/jvanegmond Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 29, 2009 Share Posted October 29, 2009 #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 More sharing options...
nikink Posted October 29, 2009 Author Share Posted October 29, 2009 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 More sharing options...
jvanegmond Posted October 29, 2009 Share Posted October 29, 2009 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) github.com/jvanegmond Link to comment Share on other sites More sharing options...
nikink Posted October 29, 2009 Author Share Posted October 29, 2009 Ah! That's very awesome to know, thankyou! Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 29, 2009 Share Posted October 29, 2009 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 More sharing options...
Moderators SmOke_N Posted October 29, 2009 Moderators Share Posted October 29, 2009 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 More sharing options...
jvanegmond Posted October 29, 2009 Share Posted October 29, 2009 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 29, 2009 Moderators Share Posted October 29, 2009 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 More sharing options...
jvanegmond Posted October 29, 2009 Share Posted October 29, 2009 I was looking for a discussion. Too bad. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Mison Posted October 30, 2009 Share Posted October 30, 2009 Thanks Manadar, I learned something new today Hi ;) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now