SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to ignore words equals in string with regex?

    I am using the following regexp to capture the data string.

    Code:
    preg_match_all('/#(\w+)/', $string, $matches);
    Ex:

    #car #CAR

    He should ignore the second.

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by theal3x View Post
    I am using the following regexp to capture the data string.

    Code:
    preg_match_all('/#(\w+)/', $string, $matches);
    Ex:

    #car #CAR

    He should ignore the second.
    Code:
    preg_match_all('/#([^#]+)/', $string, $matches);
    That should do it. Or if the only characters you want to match after the first # are letters:

    Code:
    preg_match_all('/#([a-zA-Z]+)/', $string, $matches);

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    Code:
    preg_match_all('/#([^#]+)/', $string, $matches);
    That should do it. Or if the only characters you want to match after the first # are letters:

    Code:
    preg_match_all('/#([a-zA-Z]+)/', $string, $matches);
    Works! Thank you!! =D

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the double post, but I still have a problem:

    How do I do to remove the space from the end of each match?

    If I try to use '\s' the above example does not work ...

    This in preg_replace();

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,322
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by theal3x View Post
    Sorry for the double post, but I still have a problem:

    How do I do to remove the space from the end of each match?

    If I try to use '\s' the above example does not work ...

    This in preg_replace();
    The second one will do that. Maybe digits should also be OK, then:

    Code:
    preg_match_all('/#([a-zA-Z0-9]+)/', $string, $matches);
    or amending the first one:

    Code:
    preg_match_all('/#([^# ]+)/', $string, $matches);
    In fact, if a space always follows what you want to capture, then this will do:

    Code:
    preg_match_all('/#([^ ]+)/', $string, $matches);

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    The second one will do that. Maybe digits should also be OK, then:

    Code:
    preg_match_all('/#([a-zA-Z0-9]+)/', $string, $matches);
    or amending the first one:

    Code:
    preg_match_all('/#([^# ]+)/', $string, $matches);
    In fact, if a space always follows what you want to capture, then this will do:

    Code:
    preg_match_all('/#([^ ]+)/', $string, $matches);
    Thank you again!! =D


Tags for this Thread

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
  •