Gobliins
Gobliins

Reputation: 4026

Recursive checking a string for repeating pattern?

sorry if this is a duplicate but i couldnt find anything close.

i want to check recursively a string for the following pattern

[a-z0-9][:][a-z0-9][&][a-z0-9][:][a-z0-9]...

example

foo:bar&foo:bar1&foo:bar&foo:111&bar:2A2... 

is it possible with regex and if so anyone can show me a regex expression for this?

If there is a efficient java method for this, it would be also good.

Upvotes: 0

Views: 106

Answers (3)

Dzienny
Dzienny

Reputation: 3417

Assuming that you want to match the whole string:

(\w+:\w+(?:&\w+:\w+)*)

See a demo.


Regular expression visualization

Debuggex Demo

Upvotes: 2

Akash Thakare
Akash Thakare

Reputation: 22972

If you want to match value:value& as a sole element multiple times,

(([a-z0-9]+:)([a-z0-9]+&))+

NOTE : It won't match value:value&value:,value&value&value: etc.

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

Just put the pattern inside a group with a preceding & and then make it to repeat zero or more times.

^[a-z0-9]+:[a-z0-9]+(?:&[a-z0-9]+:[a-z0-9]+)*$

Anchors won't be needed if you use matches method.

DEMO

Upvotes: 2

Related Questions