tags:

views:

152

answers:

2

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

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

Should not Match:
String 1: Hello World, Nice to see you!
String 2: Hello World, Nice to see you! #ta
String 3: #tHello World, Nice to see you!

Edit 2: Added more string samples

Edit 1 for Serg555 and SilentGhost:
Characters allowed in search string:
#[_a-zA-Z0-9]
# is optional.

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

+3  A: 

All you need is:

/(?:^|\s)#t\b/i           #t is in the beginning or preceded by space.

\b matches word border, which is a border between word-characters and non-word characters. # is a non-word character, therefore your regex matches only strings like this: abc#t, or ab_#t.

Also, normally # is not a special character in regex, you don't need to escape it.

ETA: Your requirements are rather ambiguous: 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.

  1. no white space character before or after? so there will be a non white-space character?
  2. but how is it separated then from other strings? what characters are allowed?

I think you need to give a comprehensive sample of your possible input strings. Because, as it is my regex works just fine.

SilentGhost
what if i only want it to match `#t` but not `abc#t` or `#ta`?
Jayrox
This one would also match abc#t, but it shouldn't.
serg
@serg: I don't think that OP has such a problem
SilentGhost
What do you mean? He just wrote it in the first comment.
serg
@Jayrox, serg, I've corrected the regex
SilentGhost
This one wouldn't match `abc,#t` :p
serg
Anyway, this answer is probably better now, just OP needs to manually list all characters that can be matched before the word, according to their needs.
serg
@serg, I only plan on allowing the # to be present at the start of a string.
Jayrox
serg, i've added the characters I plan on allowing to the original question.
Jayrox
@SilentGhost, I have updated the question with answers to your questions
Jayrox
@Jayrox: well, my regex works, even its previous version did.
SilentGhost
Sweet, I'll give it a try in a few.
Jayrox
A: 

Try /(?!\b)#t\b/si

serg
and this will match `t`
SilentGhost
Yep, using \b for searching non-word characters is not the best idea. But still it would make less false matches than `#t\b`
serg
@serg: uhm, right. It would fail on all inputs provided by OP
SilentGhost
this one? no, only in your case of `t`
serg