Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading