Jump to content
Sign in to follow this  
Teslafreak

Regex issue

Recommended Posts

Teslafreak

I am trying to use StringRegExp to match a UNC path, and just can't get it to work. If I try

$testserv = "\\server"
If StringRegExp($testserv,$testserv) Then
    <some code>
EndIf

That works, as does

$testserv = "^\\server$"
If StringRegExp($testserv,$testserv) Then
    <some code>
EndIf

I am really trying to anchor the start and end. I cannot get it to work as such though

$testserv = "\\server"
If StringRegExp($testserv,'^'&$testserv&'$') Then
    <some code>
EndIf

Or as the more likely (to me anyway)

$testserv = "\\server"
$teststr = "*" & $testserv & "$"
If StringRegExp($testserv, $teststr) Then
    <some code>
EndIf

I just can't seem to figure out how to concatenate the start and end anchors into a string for testing, or concatenate the whole thing in the actual function for testing. Any ideas?

Thanks in advance to everyone.

Share this post


Link to post
Share on other sites
jchd

Your current attempts amount to the following tautological:

If $testserv == $testserv Then

<some code>

EndIf

I don't know but you may be willing something esle. What exactly?


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)

Share this post


Link to post
Share on other sites
jchd

You're taking the fun out of it...


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)

Share this post


Link to post
Share on other sites
PsaltyDS

I'm sure you meant the first $testserv to be a different variable to evaluate, right?

Also, your pattern is only checking against "\server" with a single backslash because that happens to be the escape symbol for a pattern. To test double backslash, escape it twice: "\\\\server"

$testserv = "\\server"
$sPatt = "(?i)^\\\\server$"

If StringRegExp($testserv, $sPatt) Then
    ConsoleWrite("True" & @LF)
Else
    ConsoleWrite("False" & @LF)
EndIf

:idea:

You're taking the fun out of it...

Oh, sorry!

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Teslafreak

Your current attempts amount to the following tautological:

If $testserv == $testserv Then

<some code>

EndIf

I don't know but you may be willing something esle. What exactly?

This was a simplistic example (and perhaps not the best one), I get what you mean though. I will need a regex option later though.

You need to escape to escape character twice.

$testserv = "\\server"
$teststr = "\\" & $testserv
If StringRegExp($testserv, $teststr) Then
    MsgBox(0, "", "Match", 2)
EndIf

That did it, I don't know why it hadn't thought to escape those, but thanks!

Share this post


Link to post
Share on other sites
jchd

OK for the escapes, but my point remains.

Your post said "I am really trying to anchor the start and end. I cannot get it to work as such though"

If you do what you say and have the server name (with the required number of reverse-solidus) in between without anything else, that amounts to testing things much more simply:

Local $input ;; <-- say you get the server name here, from input or grabbing it from a logfile or whatever.

Local $server = "\\servername"

If $input = $server Then <some code>

I mean in regexp, the pattern "^xxx$" litterally means the source string is equal to 'xxx'.

BTW, if you find it painful to care about manually escaping backslashes and any other metacharacters in a path for instance, that you whish to include in a pattern, you can use the metacharacter-suppression metacharacters \Q and \E. In plain:

$mySubPattern = "\QNow, this string may contain balls-breaking metacharacters (like dots . stars * backslashes \ and others ] ^ + | [ $ \x00 ...) and I don't care! Do you see that?\E"


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)

Share this post


Link to post
Share on other sites
Teslafreak

I'm sure you meant the first $testserv to be a different variable to evaluate, right?

Nope, I meant to show that it was indeed matching against itself, which is part of what was confusing me, since it wouldn't have been properly escaped there. I am guessing it matched anyway because in that instance it didn't have start and end anchors, so it just matched the middle.

OK for the escapes, but my point remains.

Your post said "I am really trying to anchor the start and end. I cannot get it to work as such though"

If you do what you say and have the server name (with the required number of reverse-solidus) in between without anything else, that amounts to testing things much more simply:

Local $input ;; <-- say you get the server name here, from input or grabbing it from a logfile or whatever.

Local $server = "\\servername"

If $input = $server Then <some code>

I mean in regexp, the pattern "^xxx$" litterally means the source string is equal to 'xxx'.

BTW, if you find it painful to care about manually escaping backslashes and any other metacharacters in a path for instance, that you whish to include in a pattern, you can use the metacharacter-suppression metacharacters \Q and \E. In plain:

$mySubPattern = "\QNow, this string may contain balls-breaking metacharacters (like dots . stars * backslashes \ and others ] ^ + | [ $ \x00 ...) and I don't care! Do you see that?\E"

Again, I get it, and again, it was a crappy example on my behalf. I will need to do some more tricky stuff later between the variable and the end anchor, so I did need to know what I was missing. I'm happy that someone knew what I meant, but I would agree with you given the information. It would not only be easier to use ==, it would also likely be more efficient from a processing perspective. It just wouldn't apply later on.

Thanks again, everyone that replied.

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.