SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with (apparently) simple regex

    I am trying to write a simple regex to use with preg_match() but i keep failing.

    I am trying to match just the string 'llc' or any two-char alphabetical string.

    For example, the string
    PHP Code:
         $string 'frllcfd'
    would fail. Yes, it contains 'llc', but I want preg_match() to return true ONLY if
    PHP Code:
         $string 'llc'
    OR, I would like to match (using the same regex) any string such as the following:
    PHP Code:
         $stringA 'jj'//match
         
    $stringB 'co'//match
          
    $stringC 'MN'//match
          
    $stringD 'Ny'//match
          
    $stringE 'FoO'//fail
          
    $stringF 'bAr'//fail
         // etc....case insensitive 

    This is what I have so far:
    PHP Code:
         $pattern '([l]{2}[c]|[a-z]{2})'
    Obviously, it doesn't work the way I want it to. Any ideas?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try
    Code:
    $pattern = "^(llc|[A-Za-z]{2})$"
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes
    Try
    Code:
        $pattern = "^(llc|[A-Za-z]{2})$"
    Awesome, thank you. The only problem I have is that my flavor of php(4.3.7) throws this error:
    Code:
        Warning:  No ending delimiter '^' found in /var/www/cgi-bin/etc......
    The error goes away if I add left and right outer ():
    PHP Code:
         $pattern "(^(llc|[A-Za-z]{2})$)" 
    The regex still seems to work as expected, however. Is this ok, or will I have some other problem?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should be able to put slashes around it to get around this

    PHP Code:
    $pattern "/^(llc|[A-Za-z]{2})$/" 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes
    You should be able to put slashes around it to get around this

    PHP Code:
      $pattern "/^(llc|[A-Za-z]{2})$/" 
    Excellent! Thanks again.
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."



Bookmarks

Posting Permissions

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