non-greedy

How come my regex isn't working as expected in Bash? Greedy instead of Lazy

社会主义新天地 提交于 2020-02-06 07:55:48
问题 How come my regex pattern isn't lazy? It should be capturing the first number, not the second. Here is a working bash script.. #!/bin/bash text='here is some example text I want to match word1 and this number 3.01 GiB here is some extra text and another number 1.89 GiB' regex='(word1|word2).*?number[[:blank:]]([0-9.]+) GiB' if [[ "$text" =~ $regex ]]; then echo 'FULL MATCH: '"${BASH_REMATCH[0]}" echo 'NUMBER CAPTURE: '"${BASH_REMATCH[2]}" fi Here is the output... FULL MATCH: word1 and this

How come my regex isn't working as expected in Bash? Greedy instead of Lazy

守給你的承諾、 提交于 2020-02-06 07:53:26
问题 How come my regex pattern isn't lazy? It should be capturing the first number, not the second. Here is a working bash script.. #!/bin/bash text='here is some example text I want to match word1 and this number 3.01 GiB here is some extra text and another number 1.89 GiB' regex='(word1|word2).*?number[[:blank:]]([0-9.]+) GiB' if [[ "$text" =~ $regex ]]; then echo 'FULL MATCH: '"${BASH_REMATCH[0]}" echo 'NUMBER CAPTURE: '"${BASH_REMATCH[2]}" fi Here is the output... FULL MATCH: word1 and this

Why does a simple .*? non-greedy regex greedily include additional characters before a match?

我怕爱的太早我们不能终老 提交于 2020-01-17 12:56:06
问题 I have a very simple regex similar to this: HOHO.*?_HO_ With this test string... fiwgu_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO___HO_fbguyev I expect it to match just _HOHO___HO_ (shortest match, non-greedy) Instead it matches _HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO___HO_ (longest match, looks greedy). Why? How can I make it match the shortest match? Adding and removing the ? gives the same result. Edit - better test string that shows why [^HOHO] doesn't work: fiwgu_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO_H_O_H_O_HO

How to do the smallest match in regex replace even if the editor does not support non-greedy match

纵饮孤独 提交于 2020-01-05 18:55:18
问题 I using regex search to replace the following string: \new{}\new{\textbf{test1}}\new{test2} with \textbf{test1}test2 I used regex replace with \new{(.*)} to find and \1 to replace. however the search always match the whole line of my original string and the replace reuslt is: }\new{\textbf{test1}}\new{test2 far from what I need. In regex expression in Java, you can use a ? after a quantifier makes it a reluctant quantifier. It then tries to find the smallest match. So in java, my search regex

perl non-greedy problem

浪子不回头ぞ 提交于 2020-01-03 18:33:05
问题 I am having a problem with a non-greedy regular expression. I've seen that there are questions regarding non-greedy regex, but they don't answer to my problem. Problem: I am trying to match the href of the "lol" anchor. Note: I know this can be done with perl HTML parsing modules, and my question is not about parsing HTML in perl. My question is about the regular expression itself and the HTML is just an example. Test case: I have 4 tests for .*? and [^"] . The 2 first produce the expected

Which would be better non-greedy regex or negated character class?

做~自己de王妃 提交于 2019-12-29 08:04:31
问题 I need to match @anything_here@ from a string @anything_here@dhhhd@shdjhjs@ . So I'd used following regex. ^@.*?@ or ^@[^@]*@ Both way it's work but I would like to know which one would be a better solution. Regex with non-greedy repetition or regex with negated character class? 回答1: Negated character classes should usually be prefered over lazy matching, if possible. If the regex is successful, ^@[^@]*@ can match the content between @ s in a single step, while ^@.*?@ needs to expand for each

Which would be better non-greedy regex or negated character class?

纵饮孤独 提交于 2019-12-29 08:04:17
问题 I need to match @anything_here@ from a string @anything_here@dhhhd@shdjhjs@ . So I'd used following regex. ^@.*?@ or ^@[^@]*@ Both way it's work but I would like to know which one would be a better solution. Regex with non-greedy repetition or regex with negated character class? 回答1: Negated character classes should usually be prefered over lazy matching, if possible. If the regex is successful, ^@[^@]*@ can match the content between @ s in a single step, while ^@.*?@ needs to expand for each

Non-greedy in Python Regex

穿精又带淫゛_ 提交于 2019-12-24 13:24:38
问题 I try to understand the non-greedy regex in python, but I don't understand why the following examples have this results: print(re.search('a??b','aaab').group()) ab print(re.search('a*?b','aaab').group()) aaab I thought it would be 'b' for the first and 'ab' for the second. Can anyone explain that? 回答1: This happens because the matches you are asking match afterwards . If you try to follow how the matching for a??b happens from left to right you'll see something like this: Try 0 a plus b vs

regular expression greedy on left side only (.net)

回眸只為那壹抹淺笑 提交于 2019-12-23 04:34:07
问题 I am trying to capture matches between two strings. For example, I am looking for all text that appears between Q and XYZ, using the "soonest" match (not continuing to expand outwards). This string: circus Q hello there Q SOMETEXT XYZ today is the day XYZ okay XYZ Should return: Q SOMETEXT XYZ But instead, it returns: Q hello there Q SOMETEXT XYZ Here is the expression I'm using: Q.*?XYZ It's going too far back to the left. It's working fine on the ride side when I use the question mark after

Non greedy regex

我是研究僧i 提交于 2019-12-22 18:44:32
问题 I need to get the value inside some tags in a comment php file like this php code /* this is a comment !- <titulo>titulo3</titulo> <funcion> <descripcion>esta es la descripcion de la funcion 6</descripcion> </funcion> <funcion> <descripcion>esta es la descripcion de la funcion 7</descripcion> </funcion> <otros> <descripcion>comentario de otros 2a hoja</descripcion> </otros> -! */ some php code so as you can see the file has newlines and repetions of tags like <funcion></funcion> and i need to