Expression help please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt MC

    Expression help please

    I need to validate a string with the following requirements:

    1. Validated string must start and end with a forward-slash /
    2. Upper or lowercase is fine
    3. No numbers
    4. No special characters except for periods

    Can someone help me with this please? Any further detailed explanation
    how you put it together would be excellent too.

    Sincerely, Matt

  • Janwillem Borleffs

    #2
    Re: Expression help please

    Matt MC wrote:[color=blue]
    > 1. Validated string must start and end with a forward-slash /
    > 2. Upper or lowercase is fine
    > 3. No numbers
    > 4. No special characters except for periods
    >
    > Can someone help me with this please? Any further detailed
    > explanation how you put it together would be excellent too.
    >[/color]

    if (preg_match("#^[a-z.]+/$#i", $string)) {
    // Pattern matches the string
    }

    # = delimeter
    ^ = start of string
    [a-z.] = character class, matches characters a-z and periods
    + = one or more occurrences of the characters in the class
    $ = end of string
    # = delimeter
    i = ignore case pattern modifier

    In the character class, you can add any characters you want to allow.

    See http://www.php.net/manual/en/referen...ern.syntax.php for more
    info.


    JW



    Comment

    • Matt MC

      #3
      Re: Expression help please

      Not sure if I have it right, but it doesn't seem to pass strings
      correctly. Here is what I am using for testing:

      <?php

      $string="/something/";

      if(preg_match(" #^[a-z.]+/$#i", $string)){

      ?>

      PASSED

      <?php } else { ?>

      FAILED

      <?php } ?>


      So, that string should pass strings like:

      /something something/ (good)
      /Something Something/ (good)
      /Something. Something./ (good)

      //something something// (no)
      something / something / (no)
      something // something (no)
      /something something (no)
      /Something &@* Something/ (no)
      Something Something (no)

      I think you get the idea. Only two /'s allowed only at the start and
      end of the incoming string. Did I miss something/implementation above?

      Thank you very much for your assistance.

      Sincerely - Matt

      Comment

      • Joe Webster

        #4
        Re: Expression help please

        "Matt MC" <mattsimc@yahoo .com> wrote in message
        news:1110914968 .261967.265930@ g14g2000cwa.goo glegroups.com.. .[color=blue]
        > I need to validate a string with the following requirements:
        >
        > 1. Validated string must start and end with a forward-slash /
        > 2. Upper or lowercase is fine
        > 3. No numbers
        > 4. No special characters except for periods
        >
        > Can someone help me with this please? Any further detailed explanation
        > how you put it together would be excellent too.
        >
        > Sincerely, Matt
        >[/color]

        if(preg_match("/^\/[a-z\.\s]+\/$/i", $stringtotest)) {
        echo "Passed";
        }
        else{
        echo "Failed";
        }

        The preg breaks down like this. The first character in the preg tells the
        parser what the delimiter is to separate the preg from the options at the
        end. The standard is to use / but anything really will work. If you use a /,
        and want to use a literal /, then you must escape it with \/.

        The next character is ^, this normally means NOT, but in the case that it
        comes as the second character it means "a string that starts with...". So we
        have

        ^\/ which means "a string that starts with a literal /"

        Then we have [a-z.]\.\s This means that we want to match any letter from
        a-z, a literal . or a space. The . is normally a special character that
        means any possible character. The + sign that follows means how man of those
        to match, + means "one or more".

        [a-z\.\s]+ means "one or more characters that are either a-z, literal .
        or a space"

        Then we have \/$. This is another escaped / meaning a literal / and the $
        means "ends a line".

        \/$ means "a line that ends with a literal /"

        then we have our ending delimiter /, followed by i which means
        case-insensitive. So now a-z becomes a-zA-z, and nothing else is really
        affected.

        HTH,
        Joe


        Comment

        • Janwillem Borleffs

          #5
          Re: Expression help please

          Matt MC wrote:[color=blue]
          > Not sure if I have it right, but it doesn't seem to pass strings
          > correctly. Here is what I am using for testing:
          >
          > <?php
          >
          > $string="/something/";
          >
          > if(preg_match(" #^[a-z.]+/$#i", $string)){
          >[/color]

          Sorry, forgot the first slash. Try the following:

          if (preg_match("#^/[a-z.]+/$#i", $string)){
          ...
          }


          JW



          Comment

          • Ian Taylor

            #6
            Re: Expression help please

            [color=blue]
            > Matt MC wrote:
            > Sorry, forgot the first slash. Try the following:
            >
            > if (preg_match("#^/[a-z.]+/$#i", $string)){
            > ...
            > }
            >
            >
            > JW
            >
            >
            >[/color]

            don't you need to escape the period as well?


            i.e.

            if (preg_match("#^/[a-z\.]+/$#i", $string)){
            ...
            }

            otherwise the condition could match anything, indicated by the unescaped
            period

            Comment

            • Jan Pieter Kunst

              #7
              Re: Expression help please

              Ian Taylor wrote:
              [color=blue]
              > don't you need to escape the period as well?
              >
              >
              > i.e.
              >
              > if (preg_match("#^/[a-z\.]+/$#i", $string)){
              > ...
              > }
              >
              > otherwise the condition could match anything, indicated by the unescaped
              > period[/color]

              Within a character class (i.e. between []) much less escaping is needed
              than outside a character class. Though I can't remember at the moment
              (too lazy to look it up) if that also goes for the period.

              JP

              --
              Sorry, <devnull@cauce. org> is a spam trap.
              Real e-mail address unavailable. 5000+ spams per month.

              Comment

              • John Dunlop

                #8
                Re: Expression help please

                Ian Taylor wrote:
                [color=blue]
                > [Somebody wrote:][/color]
                [color=blue][color=green]
                > > if (preg_match("#^/[a-z.]+/$#i", $string)){[/color][/color]
                [color=blue]
                > don't you need to escape the period as well?[/color]

                No, since in a character class a full stop isn't special.



                --
                Jock

                Comment

                Working...