Regular expression greedy

I’m sure this is easy, but I tried many changes, but still can’t meet my needs. Things are too greedy, I can’t make it stop greedy.

Given the text:

test=this=that=more text follows

I want to choose:

test=

I tried the following regular expressions

(\S+)=(\S.*)
(\S+)?=
[^=]{1}
...

Thank you all.

here:

// matches "test=, test"
(\S+? )=

or

// matches "test=, test" too
(\S[^=]+)=

You should consider using the first version. Given your string “test = this = that = more text following”, version 1 will match test = this = that = and then continue to parse to the end of the string. Then it will backtrack , And find test = this =, continue backtracking, find test =, continue backtracking, and then select test = as its final answer.

Version 2 will match test = and then stop. You can use the larger I see efficiency improvements in the search, such as multi-line or entire document matching.

I am sure this is easy, but I tried many changes, but still can’t meet my needs . Things are too greedy, I can’t let it stop being greedy.

Given the text:

test=this=that=more text follows< /pre> 

I want to choose:

test=

I tried the following regular expressions

(\S+)=(\S.*)
(\S+)?=
[^=]{1}
...

Thank you all.

here:

// matches "test=, test"
(\S+? )=

or

// matches "test=, test" too
(\S[^=]+)=

You should consider using the first version. Given your string "test = this = that = more text following", version 1 will match test = this = that = and then continue to parse to the end of the string. Then it will backtrack , And find test = this =, continue backtracking, find test =, continue backtracking, and then select test = as its final answer.

Version 2 will match test = and then stop. You can use the larger See efficiency improvements in search, such as multi-line or entire document matching.

Leave a Comment

Your email address will not be published.