user1146081
user1146081

Reputation: 195

Regular Expression: How to write nested search pattern?

I'm struggling with writing RegEx pattern to find continuous sets of blocks like that:

pseudo code:

any sub-string consisted of any number of characters
finished with DDCC
repeated many times

For example I'd like to strings like this: 2342DDCC3423423DDCCfsfsfsfDDCC2weDDCC1312312qeqeDDCC to be found.

The first part is easy: [A-Za-z0-9]+DDCC

However when I did: [[A-Za-z0-9]+DDCC]+ function has returned an empty string. How to code multiple repetition of the pattern, which internally has the repetition syntax itself?

Upvotes: 0

Views: 76

Answers (2)

Ehtesham
Ehtesham

Reputation: 2985

To capture all groups you can use following expression.

([A-Za-z0-9]+?DDCC)   // use global flag based on your language/tool

It will capture all groups ending at DDCC. The important thing to note here is the use of ? after [A-Za-z0-9] which makes the matching non greedy.

Upvotes: 0

Toto
Toto

Reputation: 91415

How about:

([A-Za-z0-9]+DDCC)(?1)+

(?1) means the same pattern as the first capturing group.

Upvotes: 2

Related Questions