JREAM
JREAM

Reputation: 5931

Regex don't capture last item to stop at

I am trying to capture [dockercloud_servers] until the beginning of the next [ (the start of redis_servers). However, I don't want to capture the [ last bracket.

I tried doing ?: but I can't seem to omit that last bracket, any ideas?

Link: https://regex101.com/r/fYRiJR/1


OR:


Regex

(\[dockercloud_servers\]){1}(?:[^ ]+\[)

Content

[dockercloud_servers]
x
y
[redis_servers]
x
x

Upvotes: 0

Views: 49

Answers (2)

Vijay Wilson
Vijay Wilson

Reputation: 516

Try this

(\[dockercloud_servers\].*)(?=\[redis_servers])

https://regex101.com/r/wmBoK3/1

Upvotes: 1

Ibrahim
Ibrahim

Reputation: 6088

You just have to make a Positive Lookahead with [ at the end as the following:

(?:\[dockercloud_servers\]){1}(?:[^ ]+)(?=\[)

Demo: https://regex101.com/r/fYRiJR/3

Or alternatively, you can make your code shorter by writing the beginning of the word, and then use [\s\S]+ to capture the rest until [

\[docker[\s\S]+(?=\[)

Demo: https://regex101.com/r/fYRiJR/4

Upvotes: 1

Related Questions