RegEx - Chk for special chars

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M Kinnear

    RegEx - Chk for special chars

    I want to check a string only contains a-z 0-9 ( ) . and #

    I've used

    ereg("^[a-zA-z0-9().#]*)$"),$instr)

    which parses a string correctly until I try and parse a !,",£,$ ... or any
    of the other special/reserved characters.

    First q: what am I doing wrong
    Second q: is there clarification on whether characters need to be escaped
    when part of a regex statement? i.e., should it be [$] or [\$] - some
    websites/posts state they should be escaped, others say they shouldn't


    Thanks

    Matt


  • Benjamin

    #2
    Re: RegEx - Chk for special chars

    On Apr 29, 6:37 pm, "M Kinnear" <newsgro...@lon yx.netwrote:
    I want to check a string only contains a-z 0-9 ( ) . and #
    >
    I've used
    >
    ereg("^[a-zA-z0-9().#]*)$"),$instr)
    >
    which parses a string correctly until I try and parse a !,",£,$ ... or any
    of the other special/reserved characters.
    >
    First q: what am I doing wrong
    I think putting \ before each character should do the trick.
    Second q: is there clarification on whether characters need to be escaped
    when part of a regex statement? i.e., should it be [$] or [\$] - some
    websites/posts state they should be escaped, others say they shouldn't
    Have you looked at the PHP manual? Try http://www.php.net/manual/en/referen...ern.syntax.php
    >
    Thanks
    >
    Matt

    Comment

    • gosha bine

      #3
      Re: RegEx - Chk for special chars

      On 30.04.2007 01:37 M Kinnear wrote:
      I want to check a string only contains a-z 0-9 ( ) . and #
      >
      I've used
      >
      ereg("^[a-zA-z0-9().#]*)$"),$instr)
      This line has a syntax error. I'd suggest

      preg_match('/^[\w().#]*$/D', $instr)
      >
      which parses a string correctly until I try and parse a !,",£,$ ... or any
      of the other special/reserved characters.
      >
      First q: what am I doing wrong
      Second q: is there clarification on whether characters need to be escaped
      when part of a regex statement? i.e., should it be [$] or [\$] - some
      websites/posts state they should be escaped, others say they shouldn't
      Inside a character class ([...]) only slash \ caret ^ and dash - have
      special meaning and should be escaped. All other chars are taken
      literally. When using double quotes, you also have to escape dollar sign
      when it comes before a letter.

      >
      >
      Thanks
      >
      Matt
      >
      >

      --
      gosha bine

      extended php parser ~ http://code.google.com/p/pihipi
      blok ~ http://www.tagarga.com/blok

      Comment

      • Toby A Inkster

        #4
        Re: RegEx - Chk for special chars

        M Kinnear wrote:
        I want to check a string only contains a-z 0-9 ( ) . and #
        >
        I've used
        >
        ereg("^[a-zA-z0-9().#]*)$"),$instr)
        Firstly, it looks to me like a couple of extra brackets slipped into the
        code you posted, as I don't think the above would parse at all. I'm
        assuming what you're actually using is:

        ereg("^[a-zA-z0-9().#]*$", $instr)

        Firstly, use preg_match. It runs faster that ereg; and ereg is being
        phased out in newer versions of PHP. This gives you:

        preg_match("/^[a-zA-z0-9().#]*$/", $instr)

        Now, the dot (.) in regular expressions has a special meaning. It means
        "absolutely any character you want". This means that you are allowing
        absolutely any character at all -- that is, your regular expression is
        doing nothing. The dot needs to be escaped with a backslash:

        preg_match("/^[a-zA-z0-9()\.#]*$/", $instr)

        Also, parentheses (these) have a special meaning within regular
        expressions, marking out subexpressions. IIRC, this shouldn't be a problem
        in your case because that special meaning disappears within square
        brackets. However, it doesn't hurt to escape them, and it looks a bit
        clearer to people just glancing at the expression:

        preg_match("/^[a-zA-z0-9\(\)\.#]*$/", $instr)

        Now try it.
        Second q: is there clarification on whether characters need to be
        escaped when part of a regex statement? i.e., should it be [$] or [\$] -
        some websites/posts state they should be escaped, others say they
        shouldn't
        Generally it never hurts to escape, so when something could go either way,
        err on the side of escaping things. Note that there are two levels of
        escapes coming into play here -- firstly as you're using "double quotes"
        you need to escape against PHP's built in string-parsing. Secondly, you
        need to escape against special meanings of characters in regular
        expressions.

        To make things simpler, it is usually preferable to use 'single quotes',
        so that you only have to worry about one set of escaping.

        preg_match('/^[a-zA-z0-9\(\)\.#]*$/', $instr)

        --
        Toby A Inkster BSc (Hons) ARCS
        Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

        Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        • shimmyshack

          #5
          Re: RegEx - Chk for special chars

          On Apr 30, 9:08 am, gosha bine <stereof...@gma il.comwrote:
          On 30.04.2007 01:37 M Kinnear wrote:
          >
          I want to check a string only contains a-z 0-9 ( ) . and #
          >
          I've used
          >
          ereg("^[a-zA-z0-9().#]*)$"),$instr)
          >
          This line has a syntax error. I'd suggest
          >
          preg_match('/^[\w().#]*$/D', $instr)
          >
          >
          >
          which parses a string correctly until I try and parse a !,",£,$ ... or any
          of the other special/reserved characters.
          >
          First q: what am I doing wrong
          Second q: is there clarification on whether characters need to be escaped
          when part of a regex statement? i.e., should it be [$] or [\$] - some
          websites/posts state they should be escaped, others say they shouldn't
          >
          Inside a character class ([...]) only slash \ caret ^ and dash - have
          special meaning and should be escaped. All other chars are taken
          literally. When using double quotes, you also have to escape dollar sign
          when it comes before a letter.
          >
          >
          >
          Thanks
          >
          Matt
          >
          --
          gosha bine
          >
          extended php parser ~http://code.google.com/p/pihipi
          blok ~http://www.tagarga.com/blok
          and one more thing, the OP had
          [a-zA-z]
          I think he needs [a-zA-Z] as [A-z] allows more than upper and lower
          case letters

          Comment

          • M Kinnear

            #6
            Re: RegEx - Chk for special chars

            After posting (and less caffeine) I'd realised a few typos.

            Cheers

            "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
            news:vsogg4-q7c.ln1@ophelia .g5n.co.uk...
            >M Kinnear wrote:
            >
            >I want to check a string only contains a-z 0-9 ( ) . and #
            >>
            >I've used
            >>
            >ereg("^[a-zA-z0-9().#]*)$"),$instr)
            >
            Firstly, it looks to me like a couple of extra brackets slipped into the
            code you posted, as I don't think the above would parse at all. I'm
            assuming what you're actually using is:
            >
            ereg("^[a-zA-z0-9().#]*$", $instr)
            >
            Firstly, use preg_match. It runs faster that ereg; and ereg is being
            phased out in newer versions of PHP. This gives you:
            >
            preg_match("/^[a-zA-z0-9().#]*$/", $instr)
            >
            Now, the dot (.) in regular expressions has a special meaning. It means
            "absolutely any character you want". This means that you are allowing
            absolutely any character at all -- that is, your regular expression is
            doing nothing. The dot needs to be escaped with a backslash:
            >
            preg_match("/^[a-zA-z0-9()\.#]*$/", $instr)
            >
            Also, parentheses (these) have a special meaning within regular
            expressions, marking out subexpressions. IIRC, this shouldn't be a problem
            in your case because that special meaning disappears within square
            brackets. However, it doesn't hurt to escape them, and it looks a bit
            clearer to people just glancing at the expression:
            >
            preg_match("/^[a-zA-z0-9\(\)\.#]*$/", $instr)
            >
            Now try it.
            >
            >Second q: is there clarification on whether characters need to be
            >escaped when part of a regex statement? i.e., should it be [$] or [\$] -
            >some websites/posts state they should be escaped, others say they
            >shouldn't
            >
            Generally it never hurts to escape, so when something could go either way,
            err on the side of escaping things. Note that there are two levels of
            escapes coming into play here -- firstly as you're using "double quotes"
            you need to escape against PHP's built in string-parsing. Secondly, you
            need to escape against special meanings of characters in regular
            expressions.
            >
            To make things simpler, it is usually preferable to use 'single quotes',
            so that you only have to worry about one set of escaping.
            >
            preg_match('/^[a-zA-z0-9\(\)\.#]*$/', $instr)
            >
            --
            Toby A Inkster BSc (Hons) ARCS
            Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
            >
            * = I'm getting there!

            Comment

            Working...