|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
VBScript regex newbie question ... please help
I am new to VBscript regular expressions and would like to know if it is possible to do the folloiwng using VBScript regular expressions and if so how.
I have a string that has a number of constant substrings within it. I would like to make sure if all the substrings are present within the string, and if so a) set a matchfound or no is matchfound variable b) if no or partial match found, list the Substring entries for which the match failed. Let us say the string to verify against is "somestring1 Word1 someotherstring2 word2 someotherstring3 word3 someotherstring4" if I find that word1, word2, and word3 are found, I would declare match is found. Otherwise I would list the entries for which there was no match found, example "word1" if it was not found. I would like to avoid using VBScripts "inString". Can this be done or does vbscripts regular expression can search for one pattern at a time. Thanks. This is what I got so far ... strSearchOn = "xyz Word1 dsew12 word2 err3 word3 dfdfd" Dim objRegExpr Set objRegExpr = New regexp objRegExpr.Global = True objRegExpr.IgnoreCase = True objRegExpr.Pattern = ".*\s(word1).*\s(word2).*\s(word3).*" Dim colMatches Set colMatches = objRegExpr.Execute(strSearchOn) Wscript.Echo colMatches.count msgBox colMatches Set colMatches = objRegExp.Execute(strTest) WScript.Echo colMatches But it does not capture the matching entries. |
|
#2
|
||||
|
||||
|
You have to be careful when you use ".*", because that tends to match everything.
Given your example, of trying to capture alternate words, I would suggest something like Code:
objRegExpr.Pattern = "\w+\s(\w+)\w+\s(\w+)\w+\s(\w+)" \w+ matches the first word you want to ignore \s matches a single space (\w+) matches a word, and captures it for later use.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
#3
|
|||
|
|||
|
Quote:
Thanks for your reply Salem. I tried this strSearchOn = "xyz Word1 dsew12 word2 err3 word3 dfdfd" Dim objRegExpr Set objRegExpr = New regexp objRegExpr.Global = True objRegExpr.IgnoreCase = True ' objRegExpr.Pattern = ".*\s(word1).*\s(word2).*\s(word3).*" objRegExpr.Pattern = "\w+\s(\w+)\w+\s(\w+)\w+\s(\w+)" Dim colMatches Set colMatches = objRegExpr.Execute(strSearchOn) Wscript.Echo colMatches.count msgBox colMatches.count WScript.Echo colMatches and it turns up 1 match. however when I try to print the match string I get a type mismatch error on the line ' WScript.Echo colMatches' What am I doing wrong here? Tx again |
|
#4
|
||||
|
||||
|
1. I mucked up the regex
Code:
objRegExpr.Pattern = "\w+\s(\w+)\s(\w+)\s(\w+)" 2. You're trying to print the whole object which is colMatches > WScript.Echo colMatches I would have guessed that you need to loop over colMatches.count to print each entry of some kind of array of strings. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Regex Programming > VBScript regex newbie question ... please help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|