regex-greedy

Notepad++ non-greedy regular expressions

梦想的初衷 提交于 2019-11-30 12:23:15
问题 Does Notepad++ support non-greedy regular expressions? For example for text: abcxadc I want to get parts using pattern: a.+c And now I get whole string instead of 2 parts. I tried to use the '?' operator but without success. 回答1: Notepad++ doesn't support the lazy ? modifier. Instead, you can specify what you don't want: a[^c]+c Which specifies: match a , followed by one or more character that isn't c , followed by c . This will match abc and adc . 回答2: Update: from version 5.9 (build time

Why are regular expressions greedy by default?

旧时模样 提交于 2019-11-30 05:43:44
It seems that this is a huge source of confusion for beginners writing regular expressions, can cause hidden performance problems, and it would seem that a typical use case would be non-greedy. Is this just for legacy reasons (it was how it was first done, and every implementation copies that), or is there a reason for it? Hysterical Raisens Part of the answer may involve the origins of REs in practical computing. They were originally a theoretical concept from automata theory and formal language theory until Ken Thompson himself wrote a real implementation and used them in qed and ed(1) . The

Notepad++ non-greedy regular expressions

家住魔仙堡 提交于 2019-11-30 02:38:08
Does Notepad++ support non-greedy regular expressions? For example for text: abcxadc I want to get parts using pattern: a.+c And now I get whole string instead of 2 parts. I tried to use the '?' operator but without success. Notepad++ doesn't support the lazy ? modifier. Instead, you can specify what you don't want: a[^c]+c Which specifies: match a , followed by one or more character that isn't c , followed by c . This will match abc and adc . Update: from version 5.9 (build time Mar, 31. 2011), Notepad++ supports non greedy regular expressions (new scintilla 2.5). user1584660 I did the

Regex for matching a string literal in Java?

六眼飞鱼酱① 提交于 2019-11-29 15:42:57
I have an array of regular expressions strings. One of them must match any strings found in a given java file. This is the regex string I have so far: "(\").*[^\"].*(\")" However, the string "Hello\"good day" is rejected even though the quotation mark inside the string is escaped. I think what I have immediately rejects the string literal when it finds a quotation mark inside regardless of whether it is escaped or not. I need it to accept string literals with escaped quotes but it should reject "Hello"Good day" . Pattern regex = Pattern.compile("(\").*[^\"].*(\")", Pattern.DOTALL); Matcher

Strange behavior in regexes

Deadly 提交于 2019-11-29 14:36:06
There was a question about regex and trying to answer I found another strange things. String x = "X"; System.out.println(x.replaceAll("X*", "Y")); This prints YY. why?? String x = "X"; System.out.println(x.replaceAll("X*?", "Y")); And this prints YXY Why reluctant regex doesn't match 'X' character? There is "noting"X"nothing" but why first doesn't match three symbols and matches two and then one instead of three? and second regex matches only "nothing" s and not X ? Let's consider them in turn: "X".replaceAll("X*", "Y") There are two matches: At character position 0, X is matched, and is

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

前提是你 提交于 2019-11-29 09:08:17
How can I get all the matches in the following example: // Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*"); // Only "ab" is matched MatchCollection lazyMatches = Regex.Matches("abcd", @"ab.*?"); // How can I get all matches: "ab", "abc", "abcd" P.S.: I want to have the all matches in a generic manner. The example above is just an example. Tseng You could use something like: MatchCollection nonGreedyMatches = Regex.Matches("abcd", @"(((ab)c)d)"); Then you should have three backreferences with ab, abc and abcd. But, to be honest, this kind of regex doesn't

Why are regular expressions greedy by default?

懵懂的女人 提交于 2019-11-29 04:55:49
问题 It seems that this is a huge source of confusion for beginners writing regular expressions, can cause hidden performance problems, and it would seem that a typical use case would be non-greedy. Is this just for legacy reasons (it was how it was first done, and every implementation copies that), or is there a reason for it? 回答1: Hysterical Raisens Part of the answer may involve the origins of REs in practical computing. They were originally a theoretical concept from automata theory and formal

Regex for matching a string literal in Java?

守給你的承諾、 提交于 2019-11-28 05:53:59
问题 I have an array of regular expressions strings. One of them must match any strings found in a given java file. This is the regex string I have so far: "(\").*[^\"].*(\")" However, the string "Hello\"good day" is rejected even though the quotation mark inside the string is escaped. I think what I have immediately rejects the string literal when it finds a quotation mark inside regardless of whether it is escaped or not. I need it to accept string literals with escaped quotes but it should

Question marks in regular expressions

你说的曾经没有我的故事 提交于 2019-11-28 05:44:39
I'm reading the regular expressions reference and I'm thinking about ? and ?? characters. Could you explain me with some examples their usefulness? I don't understand them enough. thank you The key difference between ? and ?? concerns their laziness . ?? is lazy, ? is not. Let's say you want to search for the word "car" in a body of text, but you don't want to be restricted to just the singular "car"; you also want to match against the plural "cars". Here's an example sentence: I own three cars. Now, if I wanted to match the word "car" and I only wanted to get the string "car" in return , I

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

核能气质少年 提交于 2019-11-28 02:32:53
问题 How can I get all the matches in the following example: // Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*"); // Only "ab" is matched MatchCollection lazyMatches = Regex.Matches("abcd", @"ab.*?"); // How can I get all matches: "ab", "abc", "abcd" P.S.: I want to have the all matches in a generic manner. The example above is just an example. 回答1: You could use something like: MatchCollection nonGreedyMatches = Regex.Matches("abcd", @"(((ab)c)d)"); Then you