Reputation: 2134
How do I create a regular expression to match subword which start with same prefix, for example aaa
, the random word after that has random length.
aaa[randomword1]aaa[randomword2]
If I use pattern
(aaa\w+)*
it match (aaa)
and [randomword1]aaa[randomword2]
. But I want to match groups: aaa
, randomword1
, aaa
, randomword2
.
EDIT: I mean in the string may have multi times aaa
, and I need match all subword aaa_randomword_times_n
.
Upvotes: 0
Views: 463
Reputation: 16310
You can use following regular expression :
\b(aaa|(?<=\[).*?(?=\]))\b
\b..\b -> zero-width assertion word boundary to match word
aaa -> your specific word to look
| -> check for optional
(?<=[) look behind zero width assertion which checks characters after open square bracket([)
.*? : character to match
(?=])) => look ahead zero width assertion which matches characters before closing square bracket(])
Upvotes: 0