Home > OS >  Regex for words with @ prefx?
Regex for words with @ prefx?

Time:11-10

I'm looking for a regex that matches words with prefix symbol, say @word, for example, it should match @word in:

it should match @word
@word should be matched
or @word as well
and if it's the last word in a sentence followed by a symbol @word.

But it should not match if it's part of a word, e.g.:

@wordgame should not match
nor check@word

Basically I want to match exact @word as a standalone word (including the prefix).

I first tried:

\b(@word)\b

with word boundaries, it doesn't work because of @.

I also tried the regex in D-E-N's answer:

(^|\W)(@word)\b

It almost works, one minor issue is the \W for non-word characters matches the space in front of the word as well, for example:

this is a @word.

It matches ' @word' with a space before @

What should be the right regex?

Thanks!

CodePudding user response:

Because of your requirements I'm unsure the generic \W\w character classes are what's needed here, it looks like you're using properly formed English sentences.

Regex

(?<=^|\s)@word(?=$|[.\s])

Description

This answer simply checks for the following:

  • Look-backward for:
    • ^: Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
    • OR \s: A “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed)
  • Look-forward for:
    • The literal character “.”
    • OR \s: A “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed)

Further expansion

If you say also wanted to allow:

some sentence ending with an exclamation point @word!

Then you would change out [.\s] for [.\s!]

CodePudding user response:

\b is only a short for (^\w|\w$|\W\w|\w\W) (but as zero length). You can take your stuff from this to get the correct data:

(^|\W)(@word)\b

will find @word with a non-word character or line start before (\W^) and word boundary at end (\W or $)

You can test on regex101.com

CodePudding user response:

^@word$ seems to work perfectly for what you are testing. Basically, what this does is:

  1. Ensures that the phrase must start with @, per ^
  2. Ensures that word is in the list
  3. Ensures that there is nothing after word, per $

I was able to prove this out by using: https://regex101.com/

I was also able to prove that the negative cases dont match:

  • @wordgame
  • another@word

Hopefully that works for you.

  • Related