user2565496
user2565496

Reputation: 25

Regex with looping character or condition

Below sample correct data

Hello @[Reda Sel](Native:reda) Hello @[Peter K](Native:123456)

I using this regex

(\@\[.*?])(\(.*?:.*?\))

to get the data like

@[Reda Sel](Native:reda)
@[Peter K](Native:123456)

the problem is, when the input data something like this

Hello @[Reda Sel] Hello. @[Peter K](Native:123456) Testing
Hello @[Reda Sel](Native:reda) Hello @[Peter K](Native:123456)

The result is

@[Reda Sel] Hello. @[Peter K](Native:123456)
@[Reda Sel](Native:reda)
@[Peter K](Native:123456)

I need to capture the data with certain expression like the example above. But if the data is repeated, the result is not overlapping like that. And the result would be like this

@[Peter K](Native:123456)
@[Reda Sel](Native:reda)
@[Peter K](Native:123456)

Edited if I wanted to replace the data using the result value like

Hello Reda Sel

Should I use foreach and replace it one, by one, or is there any other regex options?

Thank you for your assistance.

Upvotes: 1

Views: 40

Answers (1)

hwnd
hwnd

Reputation: 70732

The main problem is the initial .*?, I would use negated character classes here instead ...

(@\[[^]]*])(\([^:]*:[^)]*\))

Demo

Upvotes: 3

Related Questions