Alex S
Alex S

Reputation: 222

Match Characters Between #s

If I have some text and then a series of hashtags xyz #abc1 def #hij #lmn I'd like to match all text and numbers, apart from trailing whitespace characters after the hashtags so the result should be:

abc1 def
hij
lmn

I've been experimenting with this but the closest I've got is #[a-zA-Z]*, which doesn't seem that close...

Upvotes: 1

Views: 38

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You can use

#[^#]*[^#\s]

See the regex demo. Details:

  • # - a # char
  • [^#]* - zero or more chars other than #
  • [^#\s] - a char other than # and whitespace.

To get the part of the match after #, either capture that part:

#([^#]*[^#\s])

and grab Group 1 value, or use a lookbehind:

(?<=#)[^#]*[^#\s]

In PCRE or Boost, you can even use \K operator:

#\K[^#]*[^#\s]

Upvotes: 1

Related Questions