capturing-group

Saving substrings using Regular Expressions

北城余情 提交于 2019-12-02 01:09:30
I'm new to regular expressions in Java (or any language, for that matter) and I'm wanting to do a find using them. The tricky part that I don't understand how to do is replace something inside the string that matches. For example, if the line I'm looking for is Person item6 [can {item thing [wrap]}] I'm able to write a regex that finds that line, but finding what the word "thing" is (as it may differ among different lines) is my problem. I may want to either replace that word with something else or save it in a variable for later. Is there any easy way to do this using Java's regex engine? Yes

Extract URL parameters with regex - repeating a capture group

旧街凉风 提交于 2019-12-01 20:36:06
I'm attempting to extract the URL parameters via regex and am sooo close to getting it to work. I even know what the problem is: my regex is stumbling on repeated capture groups. But I simply cannot figure out how to fix it. Language is PHP. My URL looks something like the one below. It can have no parameters, just one or multiple: member.php?action=bla&arg=2&test=15&schedule=16 My regex looks like this: member\.php((?:[\?|&](\w*)=(\w*))*) And my capture groups end up being: 1. action=bla&arg=2&test=15&schedule=16 2. schedule 3. 16 I cannot figure out how to capture all the parameters

How to get domain from a string using javascript regular expression

时光毁灭记忆、已成空白 提交于 2019-11-30 16:34:25
As the title suggests, I'm trying to retrieve the domain from a string using javascript regular expression. Take the following strings: String ==> Return "google" ==> null "google.com" ==> "google.com" "www.google.com" ==> "www.google.com" "ftp://ftp.google.com" ==> "ftp.google.com" "http://www.google.com" ==> "www.google.com" "http://www.google.com/" ==> "www.google.com" "https://www.google.com/" ==> "www.google.com" "https://www.google.com.sg/" ==> "www.google.com.sg" "https://www.google.com.sg/search/" ==> "www.google.com.sg" "*://www.google.com.sg/search/" ==> "www.google.com.sg" I've

How to get domain from a string using javascript regular expression

强颜欢笑 提交于 2019-11-29 23:25:22
问题 As the title suggests, I'm trying to retrieve the domain from a string using javascript regular expression. Take the following strings: String ==> Return "google" ==> null "google.com" ==> "google.com" "www.google.com" ==> "www.google.com" "ftp://ftp.google.com" ==> "ftp.google.com" "http://www.google.com" ==> "www.google.com" "http://www.google.com/" ==> "www.google.com" "https://www.google.com/" ==> "www.google.com" "https://www.google.com.sg/" ==> "www.google.com.sg" "https://www.google

Scala capture group using regex

Deadly 提交于 2019-11-29 20:23:19
Let's say I have this code: val string = "one493two483three" val pattern = """two(\d+)three""".r pattern.findAllIn(string).foreach(println) I expected findAllIn to only return 483 , but instead, it returned two483three . I know I could use unapply to extract only that part, but I'd have to have a pattern for the entire string, something like: val pattern = """one.*two(\d+)three""".r val pattern(aMatch) = string println(aMatch) // prints 483 Is there another way of achieving this, without using the classes from java.util directly, and without using unapply? Here's an example of how you can

Repeated capturing group PCRE

笑着哭i 提交于 2019-11-29 17:59:01
Can't get why this regex ( regex101 ) /[\|]?([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g captures all the input, while this ( regex101 ) /[\|]+([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g captures only |Func Input string is |Func(param1, param2, param32, param54, param293, par13am, param)| Also how can i match repeated capturing group in normal way? E.g. i have regex /\(\(\s*([a-z\_]+){1}(?:\s+\,\s+(\d+)*)*\s*\)\)/gui And input string is (( string , 1 , 2 )) . Regex101 says "a repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all

Scala regex Named Capturing Groups

丶灬走出姿态 提交于 2019-11-29 03:19:05
In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought that this was related to ( Regex Named Capturing Groups ) But since Java does not support groupnames until version 7 as I understand it ( ref ), Scala version 2.8.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6. gives me this exception: scala> val pattern = """(?<login>\w+) (?<id>\d+)""".r java.util.regex.PatternSyntaxException: Look-behind group does not have an obvio us maximum length near index 11 (?<login>\w+) (?<id>\d+) ^ at java.util.regex.Pattern.error(Pattern.java:1713) at java.util.regex

Scala capture group using regex

放肆的年华 提交于 2019-11-28 16:24:08
问题 Let's say I have this code: val string = "one493two483three" val pattern = """two(\d+)three""".r pattern.findAllIn(string).foreach(println) I expected findAllIn to only return 483 , but instead, it returned two483three . I know I could use unapply to extract only that part, but I'd have to have a pattern for the entire string, something like: val pattern = """one.*two(\d+)three""".r val pattern(aMatch) = string println(aMatch) // prints 483 Is there another way of achieving this, without

Repeated capturing group PCRE

喜你入骨 提交于 2019-11-28 13:21:36
问题 Can't get why this regex (regex101) /[\|]?([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g captures all the input, while this (regex101) /[\|]+([a-z0-9A-Z]+)(?:[\(]?[,][\)]?)?[\|]?/g captures only |Func Input string is |Func(param1, param2, param32, param54, param293, par13am, param)| Also how can i match repeated capturing group in normal way? E.g. i have regex /\(\(\s*([a-z\_]+){1}(?:\s+\,\s+(\d+)*)*\s*\)\)/gui And input string is (( string , 1 , 2 )) . Regex101 says "a repeated capturing group will

Java Pattern print capturing groups

扶醉桌前 提交于 2019-11-28 10:35:59
((\d{1,2})/(\d{1,2})/(\d{2,4})) Is there a way to retrieve a list of all the capture groups with the Pattern object. I debugged the object and all it says is how many groups there are (5). I need to retrieve a list of the following capture groups. Example of output: 0 ((\d{1,2})/(\d{1,2})/(\d{2,4})) 1 (\d{2})/(\d{2})/(\d{4}) 2 \d{2} 3 \d{2} 4 \d{4} Update: I am not necessarily asking if a regular expression exists, but that would be most favorable. So far I have created a rudimentary parser (I do not check for most out-of-bounds conditions) that only matches inner-most groups. I would like to