GAVD
GAVD

Reputation: 2134

Regex - Regular expression for repeat word with prefix

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

Answers (2)

Akash KC
Akash KC

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

Renshaw
Renshaw

Reputation: 1155

I suggest aaa(\w+)aaa(\w+), hope it will help you:)

Upvotes: 1

Related Questions