Regex to check if exact string exists including #

Posted by Jayrox on Stack Overflow See other posts from Stack Overflow or by Jayrox
Published on 2010-05-13T15:26:01Z Indexed on 2010/05/13 15:34 UTC
Read the original article Hit count: 491

Filed under:
|
|

New question As suggested by Asaph in previous question: Regex to check if exact string exists

I am looking for a way to check if an exact string match exists in another string using Regex or any better method suggested. I understand that you tell regex to match a space or any other non-word character at the beginning or end of a string. However, I don't know exactly how to set it up.

Search String: #t

String 1: Hello World, Nice to see you! #t
String 2: Hello World, Nice to see you!
String 3: #T Hello World, Nice to see you!

I would like to use the search string and compare it to String 1, String 2 and String 3 and only get a positive match from String 1 and String 3 but not from String 2.

Requirements: Search String may be at any character position in the Subject. There may or may not be a white-space character before or after it. I do not want it to match if it is part of another string; such as part of a word.

For the sake of this question: I think I would do this using this pattern: /\b\#t\b/gi
However, this is not returning the results as I would have expected.

I am able to find the exact matches for normal strings (strings where # isn't present) using:

/\b{$search_string}\b/gi

Additional info: this will be used in PHP 5

© Stack Overflow or respective owner

Related posts about regex

Related posts about php