ludocus Posted December 5, 2016 Share Posted December 5, 2016 (edited) Hi everyone, I just started using RegExpressions, so I'm still very new to this. I'm trying to write an expression that finds a match when a string is one of the following three versions, and returns an array containing the full match (or matches). test-1.5-A test-1,5-A test-11/5-A How would I do this? Thanks in advance. Edited December 7, 2016 by ludocus Link to comment Share on other sites More sharing options...
j0kky Posted December 5, 2016 Share Posted December 5, 2016 (edited) $a = StringRegExp("test-1.5-A, test-1,5-A, test-11/5-A", "test-.+?-A", 3) For $n In $a ConsoleWrite($n & @CRLF) Next Edited December 5, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
mikell Posted December 5, 2016 Share Posted December 5, 2016 (edited) #Include <Array.au3> $str = "test-1.5-A" & @crlf & _ "test-2.5-A" & @crlf & _ "test-1,5-A" & @crlf & _ "test-11/5-A" & @crlf & _ "test-11.5-A" $res = StringRegExp($str, "test-1(?:[.,]|1/)5-A", 3) _ArrayDisplay($res) Edit jOkky, the .+? is not specific enough Edited December 5, 2016 by mikell Link to comment Share on other sites More sharing options...
jchd Posted December 5, 2016 Share Posted December 5, 2016 For a more generic expression independant of the actual numeric values one could use something like this: $str = "test-1.5-A" & @crlf & _ "test-2.5-A" & @crlf & _ "test-1,5-A" & @crlf & _ "test-723,98-A" & @crlf & _ "test-11/5-A" & @crlf & _ "test-11.5-A" $res = StringRegExp($str, "test-\d+[.,/]\d*-A", 3) _ArrayDisplay($res) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
j0kky Posted December 5, 2016 Share Posted December 5, 2016 (edited) @mikell: you're right, I didn't read he needs exatly those three strings, my new version: $s = "test-1.5-A, test-1,5-A, test-11/5-A" $a = StringRegExp($s, "test-(?:1\.5|1,5|11/5)-A", 3) For $n In $a ConsoleWrite($n & @CRLF) Next which can be easily changed at need if he wants to search for more string version adding other characters between parentesis. Edited December 5, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
ludocus Posted December 5, 2016 Author Share Posted December 5, 2016 @j0kky, @jchd, @mikell: Thank you all very much! One question though. $s = "test-1.5-A, test-1,5-A, test-11/5-A" $a = StringRegExp($s, "test-(?:1\.5|1,5|11/5)-A", 3) For $n In $a ConsoleWrite($n & @CRLF) Next In your code, why do you use this part: ....?:... To me it seems this is not necessary for it to work right? Link to comment Share on other sites More sharing options...
j0kky Posted December 5, 2016 Share Posted December 5, 2016 5 minutes ago, ludocus said: n your code, why do you use this part: ....?:... To me it seems this is not necessary for it to work right? It indicates a non-captouring group, try to delete it and see what happens to console output Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
ludocus Posted December 5, 2016 Author Share Posted December 5, 2016 @j0kky I understand. Thanks! If you don't mind, I have one last question. I am trying to create a function that replaces all special characters from a string into backslashed special characters, so they are excluded in the RegExp progress. In other words, replace all of these: \ . ^ $ | [ ( { * + ? # ) with \[ORIGINAL CHAR] where [ORIGINAL CHAR] is the special character. So: hello\something.something would become hello\\something\.something I wrote this:, but it isn't working (I'm probably doing it totally wrong ) Func _Convert($string) ;Convert all special characters into usable characters for StringRegExp usage $string = StringRegExpReplace($string, "\\\.\^\$\|\[\(\{\*\+\?\#\)", "\\$") return $string EndFunc Link to comment Share on other sites More sharing options...
j0kky Posted December 5, 2016 Share Posted December 5, 2016 (edited) StringRegExpReplace("hello\something.something", "[\\\.\^\$\|\[\(\{\*\+\?\#\)\]\}]", "\\\0") Edited December 5, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
mikell Posted December 5, 2016 Share Posted December 5, 2016 You could do it like this - BTW there are other possible ways Func _Escape($string) Local $pattern_escape = "(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)" Local $res = StringRegExpReplace($string, $pattern_escape, "\\$1") Return $res EndFunc But depending of the use, you might also use the \Q...\E sequence $str = "hello\something\.*this?yes.*something" $res = StringRegExpReplace($str, '.*(\Q*this?yes.*\E).*', "$1") Msgbox(0,"", $res) Link to comment Share on other sites More sharing options...
ludocus Posted December 5, 2016 Author Share Posted December 5, 2016 Great, thanks guys! Link to comment Share on other sites More sharing options...
iamtheky Posted December 6, 2016 Share Posted December 6, 2016 would it hurt to escape all the specials? then do something simpler like: StringRegExpReplace("hello\something.something", "[^\w]", "\\\0") ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kylomas Posted December 6, 2016 Share Posted December 6, 2016 Why would this $str = "hello\something\.*this?yes.*something" $res = StringRegExpReplace($str, '.*(\Q*this?yes.*\E).*', "$1") Msgbox(0,"", $res) not be the best use in all circumstances? Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted December 6, 2016 Share Posted December 6, 2016 kylomas, IMHO you're right and the problem was taken in the wrong way. I see no interest in escaping special chars in an entry string Link to comment Share on other sites More sharing options...
jchd Posted December 6, 2016 Share Posted December 6, 2016 Maybe not but maybe. If a string will become a literal part of a pattern, then care should indeed be taken so that it is interpreted literally. One way is escaping everything (as already proposed), escaping only PCRE special chars (already proposed too), include it inside \Q ... \E (already proposed as well) BUT the string then has to be filtered and "passivated" if it contains the literal \E itself. So replacing occurences of \E by \\E and including the thing inside \Q ... \E covers all bases ... in simple cases. Yet there is a catch: this last possibility can't be used if the surrounding part of the final pattern may contain \Q ... \E itself because \Q ... \E don't nest! So you have to be aware of the use context and cautious if you don't have a clue about nor control over the content of the overall pattern. iamtheky 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) 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