regex-greedy

Python re.sub use non-greedy mode (.*?) with end of string ($) it comes greedy!

安稳与你 提交于 2019-12-19 07:28:10
问题 Code: str = '<br><br />A<br />B' print(re.sub(r'<br.*?>\w$', '', str)) It is expected to return <br><br />A , but it returns an empty string '' ! Any suggestion? 回答1: Greediness works from left to right, but not otherwise. It basically means "don't match unless you failed to match". Here's what's going on: The regex engine matches <br at the start of the string. .*? is ignored for now, it is lazy. Try to match > , and succeeds. Try to match \w and fails. Now it's interesting - the engine

Shortest match in regex from end

≯℡__Kan透↙ 提交于 2019-12-18 08:58:11
问题 Given an input string fooxxxxxxfooxxxboo I am trying to write a regex that matches fooxxxboo i.e. starting from the second foo till the last boo. I tried the following foo.*?boo matches the complete string fooxxxxxxfooxxxboo foo.*boo also matches the complete string fooxxxxxxfooxxxboo I read this Greedy vs. Reluctant vs. Possessive Quantifiers and I understand their difference, but I am trying to match the shortest string from the end which matches the regex i.e. something like the regex to

Why non-greedy quantifier sometimes doesn't work in Oracle regex?

白昼怎懂夜的黑 提交于 2019-12-17 16:37:26
问题 IMO, this query should return A=1,B=2, SELECT regexp_substr('A=1,B=2,C=3,', '.*B=.*?,') as A_and_B FROM dual But it returns whole string A=1,B=2,C=3, instead. Why? UPD: Oracle 10.2+ required to use Perl-style metacharacters in regular expressions. UPD2: More clear form of my question (to avoid questions about Oracle version and availability of Perl-style regex extension): Why ON THE SAME SYSTEM non-greedy quantifier sometimes works as expected and sometimes doesn't? This works correctly:

How to do a non-greedy match in grep?

大憨熊 提交于 2019-12-17 08:04:29
问题 I want to grep the shortest match and the pattern should be something like: <car ... model=BMW ...> ... ... ... </car> ... means any character and the input is multiple lines. 回答1: You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier ? after the quantifier. For example you can change .* to .*? . By default grep doesn't support non-greedy modifiers, but you can use grep -P to use the Perl syntax. 回答2: Actualy the .*?

How to do a non-greedy match in grep?

陌路散爱 提交于 2019-12-17 08:04:05
问题 I want to grep the shortest match and the pattern should be something like: <car ... model=BMW ...> ... ... ... </car> ... means any character and the input is multiple lines. 回答1: You're looking for a non-greedy (or lazy) match. To get a non-greedy match in regular expressions you need to use the modifier ? after the quantifier. For example you can change .* to .*? . By default grep doesn't support non-greedy modifiers, but you can use grep -P to use the Perl syntax. 回答2: Actualy the .*?

Stackoverflow in pattern matching in java

本小妞迷上赌 提交于 2019-12-13 12:24:13
问题 I tried to split a line based on spaces not enclosed between double quotes. My regex is (([\"]([^\\\"]|\\.)+[\"]|[^ ]+))+ My Code Pattern regex = Pattern.compile("(([\"]([^\\\"]|\\.)+[\"]|[^ ]+))+"); Matcher regexMatcher = regex.matcher(line); List<String> rule = new ArrayList<String>(); while(regexMatcher.find()) rule.add(regexMatcher.group()); Input for which it is failed. SecRule REQUEST_COOKIES|!REQUEST_COOKIES:/__utm/|REQUEST_COOKIES_NAMES|ARGS_NAMES|ARGS|XML:/* "(?i:\b(?:(?:s(?:t(?:d(?

.NET Regex Negative Lookahead - What am I doing wrong (round 2!) [duplicate]

旧巷老猫 提交于 2019-12-13 10:28:18
问题 This question already has an answer here : .NET Regex Negative Lookahead - what am I doing wrong? (1 answer) Closed 3 years ago . I asked this previously and used what I believe to be entirely too simple of a construct, so I'm trying again... Assuming that I have: This is a random bit of information from 0 to 1. This is a non-random bit of information I do NOT want to match This is the end of this bit This is a random bit of information from 0 to 1. This is a non-random bit of information I

Java regular expression matching two consecutive consonants

不想你离开。 提交于 2019-12-13 01:00:54
问题 I'm trying to match only strings with two consecutive consonants. but no matter what input I give to myString this never evaluates to true, so I have to assume something is wrong with the syntax of my regex. Any ideas? if (Pattern.matches("([^aeiou]&&[^AEIOU]){2}", myString)) {...} Additional info: myString is a substring of at most two characters There is no whitespace, as this string is the output of a .split with a whitespace delimiter I'm not worried about special characters, as the

RegEx only matches last item

半腔热情 提交于 2019-12-13 00:45:00
问题 I am trying to write my own syntax highlighter in sublime. I think it uses python-based regular expression. Just want to match all tokens in a row like: description str.bla, str.blub, str.yeah, str.no My regular expression looks like: regex = "(description) (str\\.[\\w\\d]+)(,\\s*(str\\.[\\w\\d]+))*" Now I expect 1 matches in group 1 ("description"), 1 match in group 2 ("str.bla") and 3 matches in my group no 4 ("str.blub", "str.yeah", "str.no") but I have only 1 match in my last group ("str

regex greedy problem (C#)

痴心易碎 提交于 2019-12-12 10:43:28
问题 I've a input string like "===text=== and ===text===" and I want to replace wiki syntax with the corresponding html tag. input: ===text=== and ===text=== desirable output: <h1>text</h2> and <h1>text</h2> but with the following code I get this output: var regex = new Regex("---(.+)---"); var output = regex.Replace("===text=== and ===text===", "<h1>$1</h1>"); <h1>text=== and ===text</h1> I know the problem is that my regex matches greedy. But how to make them non greedy. Thank you and kind