Regex to check if an exact string exists, including #

New question Asaf suggested in the previous question: Regex to check if the exact row exists

I am looking for a way to check if there is an exact match of lines on another line using Regex or any best suggested method. I understand that you specify a regular expression according to a space or any other character other than a word, at the beginning or end of a line. However, I don’t know exactly how to configure it.

Search line: #t

Must match:
Line 1: Hello World, Nice to see you! #t
Line 2: #T Hello World, Nice to see you!
Line 3:Hello World, #t Nice to see you!

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

Edit 2: added more row patterns

Change 1 for Serg555 and SilentGhost:
Characters allowed in the search bar:
#[_a-zA-Z0-9]
#optional.

Requirements: The search string can be at any character position in the object. There may or may not be a space character before or after it. I do not want it to match if it is part of another line; for example, part of a word.

For the sake of this question: I think I will do it with this template: /\b\#t\b/gi
However, this does not return results as I expected.

I can find exact matches for regular strings (strings where # is missing) using:

/\b{$search_string}\b/gi

Additional info: this will be used in PHP 5

+5
source share
2 answers

All you need is:

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

\b , , . # , : abc#t ab_#t.

, # , .

ETA. : . , , ; .

  • ? ?
  • ? ?

, . , .

+5

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

0

All Articles