Regex greedy question

I am sure it is easy, but I tried a lot of variations and still can not match what I need. The thing is too greedy, and I can't make it stop being greedy.

Given the text:

test=this=that=more text follows

I want to just choose:

test=

I tried the following regex

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

Thanks to everyone.

+3
source share
6 answers

here:

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

or

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

you should consider using the second version with the first. given your line "test=this=that=more text follows", version 1 will match test=this=that=, and then continue parsing to the end of the line. then he will back off and find test=this=, continue the rollback and find test=, continue the indentation and return on test=as the final answer.

2 test=, . , .

+11

, -

^ (\ S +? =)

^ .? + + .

+4

, *?, +?,?? {n, n}?

+3

:

(\S+?)=(\S.*)
+1

, - .

, " , , ".

([^=]+)=([^=]+)

[^=]{1} , .

+1

"text =", , :

^(\w+=)

, , "text =" .

, :

this = that = more test = text follows

if you use the regex above the result, this is "this =", and if you change above with reapeater qualifiers at the end, for example:

^(\w+=)*

you find the huge "this = that =", so I could only imagine the trivial:

[th\w+=]*test=

Bye

0
source

Source: https://habr.com/ru/post/1698369/


All Articles