开发者

Regex assistance

Note To Self; This part of the string should not be changed because it contains note to self and NTS
Note To Self ; This part of the string should not be changed because it contains note to self and NTS
Note To Self : This part of the string should not be changed because it contains note to self and NTS
Note To Self: This part of the string should not be changed because it contains note to self and NTS
Note To Self - This part of the string should not be changed because it contains note to self and NTS
Note To Self- This part of the string should not be changed because it contains note to self and NTS
Note To Self This part of the string should not be changed because it contains note to self and NTS
NTS ; This part of the string should not be changed because it contains note to self and NTS
NTS; This part of the string should not be changed because it contains note to self and NTS
NTS : This part of the string should not be changed because it contains note to self and NTS
NTS: This part of the string should not be changed because it contains note to self and NTS
NTS- This part of the string should not be changed because it contains note to self and NTS
NTS - This part of the string should not be changed because it contains note to self and NTS
NTS This part of the string should not be changed because it contains note to self and NTS
This part of 开发者_StackOverflow中文版the string should not be changed because it contains note to self and NTS #NTS

Above is the text I am using to test my regex.

I am using this with PHP, and am just trying to extract the string without the prefix.

Essentially, what I am looking for is extracting this string 'This part of the string should not be changed because it contains note to self and NTS' from the given prefixes.

Your help is much appreciated, thanks!

EDIT: by request info was changed.

http://regexr.com?2u3jn


To remove all instances of Note to self or NTS if they are at the start of the line, optionally followed by whitespace and/or punctuation, and to remove #NTS if it's at the end of a line, you can search for

(^(Note to self|NTS)\W*|#NTS$)

and replace with nothing.

Explanation in the form of a PHP code snippet:

$result = preg_replace(
    '/(             # Either match...
     ^              #  start of line, followed by...
     (              #   either...
      Note to self  #   Note to self
     |              #   or
      NTS           #   NTS
     )              #  followed by...
     \W*            #  any number of non-alphanumeric characters
    |               # or
     \#NTS          #  match #NTS
     $              #  if it\'s the last thing before the end of the line
    )               # End of the alternation.
    /x', 
    '', $subject);


For testing of the RegEx online you can use this: http://gskinner.com/RegExr/. About your RegEx: () - group symbols. And within this main group you try to find:

  1. \A(Note to self)
  2. (NTS)

The OR here is by |

So For selecting all needed just remove the \A - bell character (\x07) from your search ((Note to self)|(NTS)). Reference: http://www.regular-expressions.info/reference.html


Try this:

^#?([Nn]ote [Tt]o [Ss]elf|NTS).*

Explanation:

  • ^ indicates the start of the line (supported in more languages than \A)
  • #? looks for 0 or 1 # symbols
  • ([Nn]ote [Tt]o [Ss]elf|NTS) is an or statement, looking for either [Nn]ote [Tt]o [Ss]elf or NTS. The [Nn] in brackets means to find either N or n (allowing you to match both "Note To Self" and "note to Self")
  • .* at the end just matches the rest of the line: . is any character, and * is any number of repetitions.

Possibly useful references:

http://www.regular-expressions.info/refflavors.html

http://docs.python.org/library/re.html (even if you're not doing Python, I think the examples and explanations are helpful)


\A is a zero-width assertion that matches the beginning of the string. You'll want to remove it.

All you need is this:

Note to self|NTS
/Note to self|NTS/i                  for PHP (case insensitive)

Though if you didn't want to match "RED ANTS", you could do this:

\bNote to self\b|\bNTS\b
/\bNote to self\b|\bNTS\b/i          for PHP (case insensitive)
/\bNote to self\b|\b(?<!#)NTS\b/i    also ignores #NTS
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜