Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to match a string without whitespaces between two #

Tags:

regex

I am looking for a regex, which returns a string between two hashtags, but only if between the two # there is no whitespace.

My current regex: \#(.*?)\#

Testcase 1:

'#thisshouldbeparsed# #2 test'
=> thisshouldbeparsed (this is currently correct)

Testcase 2:

'test #2 #thisshouldbeparsed#'

=>2 (this is not correct)

I need a regex which is returning also "thisshouldbeparsed".

like image 253
SebastianOS Avatar asked Nov 18 '25 10:11

SebastianOS


1 Answers

You can use

#([^#\s]*)#

To avoide empty matches, replace * with +:

#([^#\s]+)#

See the regex demo.

Details

  • # - a # char
  • ([^#\s]*) - Group 1: any zero or more chars other than # and whitespace
  • # - a # char.
like image 95
Wiktor Stribiżew Avatar answered Nov 21 '25 00:11

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!