regex-group

RegEx for capturing a repeating pattern

血红的双手。 提交于 2019-12-11 05:22:34
问题 I have the following regex from regex capturing with repeating pattern ([0-9]{1,2}h)[ ]*([0-9]{1,2}min):[ ]*(.*(?:\n(?![0-9]{1,2}h).*)*) It takes the following string 1h 30min: Title - Description Line 1 1h 30min: Title - Description Line 1 - Description Line 2 - Description Line 3 And produces this as a result Match 1: "1h 30min: Title - Description Line 1" Group 1: "1h" Group 2: "30min" Group 3: "Title - Description Line 1" Match 2: "1h 30min: Title - Description Line 1 - Description Line 2

regex if capture group matches string

一笑奈何 提交于 2019-12-11 00:50:24
问题 I need to build a simple script to hyphenate Romanian words. I've seen several and they don't implement the rules correctly. var words = "arta codru"; Rule: if 2 consonants are between 2 vowels, then they become split between syllables unless they belong in this array in which case both consonants move to the second syllable: var exceptions_to_regex2 = ["bl","cl","dl","fl","gl","hl","pl","tl","vl","br","cr","dr","fr","gr","hr","pr","tr","vr"]; Expected result: ar-ta co-dru The code so far:

why adding a space after `(.+?)` can completely change the result

坚强是说给别人听的谎言 提交于 2019-12-10 22:13:42
问题 I'm trying to find a smaller string, String patternString1 = "(John) (.+?)"; , within a larger string. The smaller string are consist of two groups i.e. (John) (.+?) . However, I have obtained completely different result just by adding a space after (.+?) . for String patternString1 = "(John) (.+?)"; , (i.e. without space), the result is found: John w found: John D found: John W For String patternString1 = "(John) (.+?) "; , (i.e. with space), the result is found: John writes found: John Doe

Capture group multiple times

纵饮孤独 提交于 2019-12-10 17:03:49
问题 Lately I have being playing around with regex in Java, and I find myself into a problem which (theoretically) is easy to solve, but I was wandering if there is any easier way to do it (Yes, yes I am lazy), the problem is capture a group multiple times , this is: public static void main(String[] args) { Pattern p = Pattern.compile("A (IvI(.*?)IvI)*? A"); Matcher m = p.matcher("A IvI asd IvI IvI qwe IvI A"); //ANY NUMBER of IvI x IvI //Matcher m = p.matcher("A A"); int loi = 0; //last

How to make a group for each word in a sentence?

Deadly 提交于 2019-12-10 16:53:25
问题 This may be a silly question but... Say you have a sentence like: The quick brown fox Or you might get a sentence like: The quick brown fox jumped over the lazy dog The simple regexp (\w*) finds the first word "The" and puts it in a group. For the first sentence, you could write (\w*)\s*(\w*)\s*(\w*)\s*(\w*)\s* to put each word in its own group, but that assumes you know the number of words in the sentence. Is it possible to write a regular expression that puts each word in any arbitrary

Overlapping group capturing

与世无争的帅哥 提交于 2019-12-10 15:56:16
问题 Please take a look at the following code: public static void main(String[] args) { String s = "a < b > c > d"; String regex = "(\\w\\s*[<>]\\s*\\w)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); int i = 0; while (m.find()) System.out.println(m.group(i++)); } The output of the above program is: a < b, c > d But I actually expect a < b, b > c, c > d . Anything wrong with my regexp here? 回答1: Try this. String s = "a < b > c > d"; String regex = "(?=(\\w{1}\\s{1}[<>]{1}\\s{1}\\w

Kodos & “cannot refer to open group”

女生的网名这么多〃 提交于 2019-12-10 15:17:51
问题 I want to only match 1010 or 0101 but nor 1111 nor 0000. I use the following regex : \b((1|0)(?!\2))+ It works well in Kodos but I also want the matched sequence thanks to group(). I've tried : \b(((1|0)(?!\2))+) but "cannot refer to open group*" is displayed in Kodos and I don't understand why it doesn't work. Please can you help me ? Edit: The appropriate regex is (\b(((1|0)(?!\3))+) . 回答1: I believe the problem is that you have three separate groups in your second code line. They are

Javascript RegExp non-capturing groups

[亡魂溺海] 提交于 2019-12-10 12:36:56
问题 I am writing a set of RegExps to translate a CSS selector into arrays of ids and classes. For example, I would like '#foo#bar' to return ['foo', 'bar']. I have been trying to achieve this with "#foo#bar".match(/((?:#)[a-zA-Z0-9\-_]*)/g) but it returns ['#foo', '#bar'], when the non-capturing prefix ?: should ignore the # character. Is there a better solution than slicing each one of the returned strings? 回答1: You could use .replace() or .exec() in a loop to build an Array. With .replace() :

Java Matcher groups: Understanding The difference between “(?:X|Y)” and “(?:X)|(?:Y)”

浪尽此生 提交于 2019-12-08 18:56:30
问题 Can anyone explain: Why the two patterns used below give different results? (answered below) Why the 2nd example gives a group count of 1 but says the start and end of group 1 is -1? public void testGroups() throws Exception { String TEST_STRING = "After Yes is group 1 End"; { Pattern p; Matcher m; String pattern="(?:Yes|No)(.*)End"; p=Pattern.compile(pattern); m=p.matcher(TEST_STRING); boolean f=m.find(); int count=m.groupCount(); int start=m.start(1); int end=m.end(1); System.out.println(

Extract multiple values if present from cell in Google Spreadsheets

蹲街弑〆低调 提交于 2019-12-08 04:40:54
问题 Using Google re2 https://github.com/google/re2/blob/master/doc/syntax.txt From a couple of lines like I love Rock I love Rock and scissors I hate paper I like Rock, paper and scissors I'd love myself I want to extract "Rock", "paper"and "scissors" from each line. I want the regex to match all the above five lines and give me Rock, paper and scissors where it found something. I'm predominantly using this in Google sheets, but any Google re2 regex should help. I've tried.... ".*(([Rock]{0,4})).