Jump to content

Simple StringRegExp question [SOLVED]


ludocus
 Share

Recommended Posts

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 by ludocus
Link to comment
Share on other sites

$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 by j0kky
Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

@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 by j0kky
Link to comment
Share on other sites

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 :) 

Link to comment
Share on other sites

@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 :sweating:)

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

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

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

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

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.

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 here
RegExp tutorial: enough to get started
PCRE 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

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