RegExp question

Results 1 to 2 of 2

Thread: RegExp question

  1. #1
    Join Date
    Dec 1969

    Default RegExp question

    I have a simple script to verify if an filepath contains an ip address. Sometimes it works, sometimes it doesn&#039;t. I have not idea why though... <BR><BR>Here is the script<BR><BR>sPath = WScript.Arguments.Item(0)<BR>Set oRE = new RegExp<BR>oRE.IgnoreCase = true<BR>oRE.Global = True<BR>oRE.Pattern = "([0-255]{1}.?){4}"<BR>bContainsIP = oRE.Test(sPath)<BR>Set oRE = Nothing<BR><BR>if bContainsIP Then<BR> WScript.echo "it has an IP address"<BR>Else<BR> WScript.echo "it doesn&#039;t have an IP address"<BR>End If<BR><BR><BR>Here are some examples of addresses that i&#039;ve tried. <BR>C:&#062;isip.vbs \ rippakimageslah hisimage.tif<BR><BR>it doesn&#039;t have an IP address &#060;-- incorrect<BR><BR>C:&#062;isip.vbs \25.369.258.21 <BR><BR>it doesn&#039;t have an IP address &#060;-- this is correct<BR><BR>C:&#062;isip.vbs \<BR><BR>it has an IP address &#060;-- correct <BR><BR>C:&#062;isip.vbs \25.369.258.21<BR><BR>it doesn&#039;t have an IP address &#060;-- correct<BR><BR>C:&#062;isip.vbs \ rippakimageslah hisimage.tif<BR><BR>it doesn&#039;t have an IP address &#060;-- incorrect?!<BR><BR>C:&#062;isip.vbs \\<BR><BR>it has an IP address<BR><BR>C:&#062;isip.vbs<BR><BR>it has an IP address<BR><BR>C:&#062;isip.vbs<BR><BR>it doesn&#039;t have an IP address &#060;-- incorrect?!<BR><BR><BR>So I&#039;m dumbfounded. Why would it work sometimes, and not others? <BR>Thanks,<BR>SamC

  2. #2
    Join Date
    Dec 1969

    Default That RegExp makes no sense...

    Let&#039;s analyze it:<BR><BR>oRE.Pattern = "([0-255]{1}.?){4}"<BR><BR>Start with [0-255].<BR><BR>You seem to be under the impression that you can specify a *number* in there and have it work! Nothing could be further from the truth! Those are *characters*.<BR><BR>[0-2] means any *character* in the range 0 through 2. Or, in other words, 0, 1, or 2. <BR><BR>And then a sequence like [abc] means "any of the characters given." So we could have written [0-2] as [012] for the same result.<BR><BR>Now, go back to your [0-255]. I would *expect* that that would be seen as *identical* to [01255]. The repeated 5 is ignored, so it&#039;s really the same as [0125]. And *that* means that any sequence that has any *characters* other than 0, 1, 2, or 5 will be rejected.<BR><BR> Nope. It has a 3 and a 4 in it. Reject.<BR><BR> Sure. Nothing but 1&#039;s, 2&#039;s, and 5&#039;s.<BR><BR>But then WHY is ** accepted???? It clearly has a 6 and a 3 in it, right?<BR><BR>Okay, let&#039;s continue our analysis of the RegExp.<BR><BR>We&#039;ve already converted it from<BR>&nbsp; &nbsp; &nbsp; ([0-255]{1}.?){4}<BR>to<BR>&nbsp; &nbsp; &nbsp; ([0125]{1}.?){4}<BR><BR>Let&#039;s look at ".?". What does *that* mean? The docs say "? Matches the preceding character zero or one time." ZERO OR ONE TIME!!! *NOT* one time exactly! ZERO TIMES or more.<BR><BR>And then look at {1}. Again, the docs say "{n} ...Matches exactly n times."<BR><BR>So if we take that together, we get<BR>&nbsp; &nbsp; &nbsp; ([0125]{1}.?)<BR>"Find one of the characters 0, 1, 2, or 5. Match that character 1 time, exactly. Then look for a period. Any number of periods--zero or more--will match."<BR><BR>And all of *that* means that any of the following sequences would match:<BR>&nbsp; &nbsp; &nbsp; 0<BR>&nbsp; &nbsp; &nbsp; 0.<BR>&nbsp; &nbsp; &nbsp; 0......<BR>&nbsp; &nbsp; &nbsp; 1<BR>&nbsp; &nbsp; &nbsp; 1.<BR>&nbsp; &nbsp; &nbsp; 1...................<BR>&nbsp; &nbsp; &nbsp; 2 <BR>&nbsp; &nbsp; &nbsp; 2.<BR>&nbsp; &nbsp; &nbsp; 2...<BR>&nbsp; &nbsp; &nbsp; 5<BR>&nbsp; &nbsp; &nbsp; 5.<BR>&nbsp; &nbsp; &nbsp; 5.........<BR>(and many, many more, of course)<BR><BR>Right? <BR><BR>So *finally* we come to the {4} in your RegExp pattern:<BR>&nbsp; &nbsp; &nbsp; ([0125]{1}.?){4}<BR>*That* says "match all the preceding exactly 4 times."<BR><BR>And, looking above at things that could match the first part, that means that these would all match (remember, 4 combinations of any of the things above):<BR><BR>&nbsp; &nbsp; &nbsp; 0000<BR>&nbsp; &nbsp; &nbsp;<BR>&nbsp; &nbsp; &nbsp; 00..00..<BR>&nbsp; &nbsp; &nbsp; 000.0<BR>&nbsp; &nbsp; &nbsp; 0.........0.........0...........0.........<BR>&nbs p; &nbsp; &nbsp; 2000<BR>&nbsp; &nbsp; &nbsp; 2111<BR>&nbsp; &nbsp; &nbsp; 2222<BR>&nbsp; &nbsp; &nbsp; 2555<BR>&nbsp; &nbsp; &nbsp; 2020<BR>&nbsp; &nbsp; &nbsp; 2121<BR>&nbsp; &nbsp; &nbsp; 2525<BR>&nbsp; &nbsp; &nbsp; 2..5..2..5..<BR>&nbsp; &nbsp; &nbsp; 2.52.5.....<BR>&nbsp; &nbsp; &nbsp; 25.25.....<BR><BR>Uh oh! Look at that last one! Now compare it to this:<BR>&nbsp; &nbsp; &nbsp;<BR><BR>*Now* do you see why was accepted? Because 25.25. (or even 25.25) was accepted!<BR><BR>SO...<BR><BR>Time to start all over! Try all over again. *THIS* time, think of *characters*, no numbers.<BR><BR>********************************** *<BR><BR>Off the top of my head, I think this is a starter:<BR><BR>[0-9] -- any single digit<BR>[0-9][0-9] -- any pair of digits<BR>[01][0-9][0-9] -- any 3 digits that start with 0 or 1<BR>2[0-4][0-9] -- if it starts with 20 to 24, last digit is 0 to 9<BR>25[0-5] -- if it starts with 25, last digit is 0 to 5.<BR>. -- must have a period<BR>{3} first 3 segments identical.<BR>last segment repeated, but no period allowed/required.<BR><BR>([0-9]&#124[0-9][0-9]&#124[01][0-9][0-9]&#124 2[0-4][0-9]&#124 25[0-5].){3}([0-9]&#124[0-9][0-9]&#124[01][0-9][0-9]&#124 2[0-4][0-9]&#124 25[0-5])<BR><BR>Okay. Try *that* now!<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts