No announcement yet.

Regular Expression Puzzles

  • Filter
  • Time
  • Show
Clear All
new posts

    Regular Expression Puzzles

    I want a PBasic regular expression to recognise any slash, hyphen or
    space as a separator so I defined an equate as follows:

    (1) $Sep = "[/- ]"

    This did not work because / needs to be escaped. So I wrote

    (2) $Sep = "[//- ]"

    This worked for / but not for -. I tried escaping the -

    (3) $Sep = "[///- ]"

    But that didn't work. But this did:

    (4) $Sep = "[-// ]"

    In other words, the actual order of characters within [] is apparently
    significant. Or am I misunderstanding something here.

    My question is: Why does (4) work, but not (3) or (2)?

    Any help would be most appreciated.


    I presume that you mean "\" there, because that's the escape character for regular expressions. In any case, the problem with your expression is that the dash is used to specify a range; if it's between two characters then the regex parser is going to think that you're asking for something in the range of those two characters (e.g.: [a-z]). The correct regex syntax for that kind of a match would be "[-/ ]" or [-/\\ ]" if you wanted backslashes as well.

    Edit: You might also want to consider using hex escapes for whitespace characters, just to make it clearer what you're doing in your source, and to prevent mistakes like the space being accidentally removed.
    Last edited by Mike Stefanik; 18 Jul 2008, 02:03 AM.
    Mike Stefanik


      Re: RegExp

      Mike, you are absolutely correct. Thank you. I guess I was suffering brain fatigue and not thinking straight. I'm back on track now. Much appreciated.