Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex capture group ( ) within character set [ ]

Tags:

regex

I would like to match space characters () only if they are followed by a hash (#).

This is what ( #) below is trying to do, which is a capture group. (I tried escaping the brackets, otherwise the brackets are not recognised properly within a group set). However, this is not working.

The below regex

/#[a-zA-Z\( #\)]+/g

matches all of the below

#CincoDeMayo #Derby party with UNLIMITED #seafood towers

while I would like to match #CincoDeMayo #Derby and separately #seafood

Is there any way to specify captures groups () within a character set []?

like image 609
M3RS Avatar asked Apr 24 '18 22:04

M3RS


1 Answers

Character classes are meant to match a single character, thus, it is not possible to define a character sequence inside a character class.

I think you want to match specific consecutive hashtags. Use

/#[a-zA-Z]+(?: +#[a-zA-Z]+)*/g

or

/#[a-zA-Z]+(?:\s+#[a-zA-Z]+)*/g

See the regex demo.

Details

  • #[a-zA-Z]+ - a # followed with 1+ ASCII letters
  • (?: - start of a non-capturing group...
    • \s+ - 1+ whitespaces
    • #[a-zA-Z]+ - a # followed with 1+ ASCII letters
  • )* - ... that repeats 0 or more times.
like image 99
Wiktor Stribiżew Avatar answered Oct 03 '22 06:10

Wiktor Stribiżew