Jump to content

StringRegExp


Recommended Posts

Regexp, as it has done for 20 years, is beating up on me again.

I have the system path value: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin.

If I find 'C:\PHP' I want to change it to 'D:\PHP' or actually $dvDrive & '\PHP' . Here in my example I see I have the added problem of paths for C and D drives. I was hoping that StringInStr would return a string that I could examine further but since that is not the case I decided to start with StringRegExp. Using the snippet below I cannot get a return value other than 0 from StringRegExp. I am sure it is a simple mistake but I am not seeing it.

It also appears that StringInStr does not support wild cards. Is that true?

Thanks,

jh

$regPath = 'HKLM\SYSTEM\CONTROLSET001\CONTROL\SESSION MANAGER\ENVIRONMENT'
;$s = RegRead($regPath, 'Path')
;Line above returns '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'

$s = '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'
$search = 'D:\PHP' ;'?:\PHP'
$match = StringRegExp($s, $search)
MsgBox(0,$s, $match)
Edited by hmsSurprise
Link to comment
Share on other sites

Backslash is a special character. If you want to match an actual backslash, you must escape it by using another backslash, as in "D:\\PHP".

You are correct in saying that StringInStr only does literal string matching. If you want wildcards, well, that's what StringRegExp is for. :shocked:

Edited by Sokko
Link to comment
Share on other sites

Backslash is a special character. If you want to match an actual backslash, you must escape it by using another backslash, as in "D:\\PHP".

Thanks, Sokko.

I knew it was simple or stupid or both, I should have known better, actually I do or did.

As the saying goes: 'Sometimes is better to keep your mouth shut and appear stupid than to open it and remove all doubt'.

The would-be scripter needs more starter fluid, another cup of coffee please.

jh

Link to comment
Share on other sites

Regexp, as it has done for 20 years, is beating up on me again.

I have the system path value: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin.

If I find 'C:\PHP' I want to change it to 'D:\PHP' or actually $dvDrive & '\PHP' . Here in my example I see I have the added problem of paths for C and D drives. I was hoping that StringInStr would return a string that I could examine further but since that is not the case I decided to start with StringRegExp. Using the snippet below I cannot get a return value other than 0 from StringRegExp. I am sure it is a simple mistake but I am not seeing it.

It also appears that StringInStr does not support wild cards. Is that true?

Thanks,

jh

$regPath = 'HKLM\SYSTEM\CONTROLSET001\CONTROL\SESSION MANAGER\ENVIRONMENT'
;$s = RegRead($regPath, 'Path')
;Line above returns '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'

$s = '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'
$search = 'D:\PHP' ;'?:\PHP'
$match = StringRegExp($s, $search)
MsgBox(0,$s, $match)oÝ÷ Ûú®¢×k&Þjëh×6$s = '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'
$search = 'D:\PHP' ;'?:\PHP'
If StringInStr($s, $search) Then
MsgBox(0,$s, "Found- " & $search)
Else
MsgBox(0,$s, "Not Found- " & $search)
EndIf
EndFuncAutoIt is the shiznit. I love it.
Link to comment
Share on other sites

Maybe...

$s = '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PHP;;D:\PHP;D:\MySQL\MySQL Server 4.1\bin'
$search = 'D:\PHP' ;'?:\PHP'
If StringInStr($s, $search) Then
MsgBox(0,$s, "Found- " & $search)
Else
MsgBox(0,$s, "Not Found- " & $search)
EndIf

Tried that with $search = '?:\PHP'.

Since it wouldn't treat ? as a wildcard I started looking at regexp. One solution is to just find ':\PHP', then extract the substring starting one position b4 this and evaluate the first character of the substring.

jh

Link to comment
Share on other sites

Since it wouldn't treat ? as a wildcard I started looking at regexp. One solution is to just find ':\PHP', then extract the substring starting one position b4 this and evaluate the first character of the substring.

A period in regex is equivalent to the old DOS wildcard "?". ".:\\PHP" would match any single character followed by ":\PHP". You may want to use "\w" instead, which will only match letters and the underscore, as in "\w:\\PHP". Also, look at the possible values for the "flag" parameter for StringRegExp in the help file, you can have it return the matched text in an array.

Edited by Sokko
Link to comment
Share on other sites

A period in regex is equivalent to the old DOS wildcard "?". ".:\\PHP" would match any single character followed by ":\PHP". You may want to use "\w" instead, which will only match letters and the underscore, as in "\w:\\PHP". Also, look at the possible values for the "flag" parameter for StringRegExp in the help file, you can have it return the matched text in an array.

Thanks again, you have been quite helpful. I will certainly save this for future use. I actually solved it another way.

$found = StringInStr($s, ':\PHP')
if $found Then 
    $s = StringReplace ( $s, ($found -1), $dvDrive )
Else
    ;;Append to string.
EndIf
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...