How can I have multiple optional groups in RegEx
By : user3364324
Date : March 29 2020, 07:55 AM
I wish did fix the issue. (Abbreviated) regular expression to match multiple options, of which the whole group is optional: code :
( -(pause|start|download [^ ]+))?
-pause
-start
-download filename
// matches nothing too
-pause -start
|
python regex multiple optional capture groups
By : matthew brookes
Date : March 29 2020, 07:55 AM
I hope this helps you . Regex is not the best tool for parsing in this case, I suppose there are tool exactly for that. However with given examples, you can try this: code :
<a title="(.+?)\s?((Vol(\d+))?\s?\.?(Ch.(\d+)))?"\shref="(.+)">
|
Java Regex to Parse a Path Into Multiple Optional Groups
By : yinswenti
Date : March 29 2020, 07:55 AM
will help you I am trying to split this type string using a Java Regex: , The following regex will match what you defined: code :
^(/api)?(/v\d+)?(/[^/]+(?:/[^/]+)*?)??(?:(/category[12])(/.*)?)?$
String text = "/api/v2/client/domain/category2/id";
String pattern = "^(/api)?(/v\\d+)?(/[^/]+(?:/[^/]+)*?)??(?:(/category[12])(/.*)?)?$";
Pattern regex = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher m = regex.matcher(text);
while (m.find())
{
System.out.println("api: " + m.group(1) +
"\nversion: " + m.group(2) +
"\nclient: " + m.group(3) +
"\ncategory: " + m.group(4) +
"\nextra: " + m.group(5));
}
api: /api
version: /v2
client: /client/domain
category: /category2
extra: /id
|
Regex: Capture multiple optional groups with a single match
By : J. Doe
Date : March 29 2020, 07:55 AM
this one helps. As Wiktor correctly states, there is no way to do this with a single regex. Here is a simple function that implements a 3-regex solution: code :
function get_time_parts(text) {
var s, m, h;
// Seconds part: Either "s", "sec", "secs" "second" or "seconds".
s = text.match(/\b(\d+)\s*s(?:ec(?:ond)?s?)?\b/i);
s = s ? s[1] : undefined;
// Minutes part: Either "m", "min", "mins" "minute" or "minutes".
m = text.match(/\b(\d+)\s*m(?:in(?:ute)?s?)?\b/i);
m = m ? m[1] : undefined;
// Hours part: Either "h", "hr", "hrs" "hour" or "hours".
h = text.match(/\b(\d+)\s*h(?:rs?|ours?)?\b/i);
h = h ? h[1] : undefined;
return (s || m || h) ? [s, m, h] : null;
}
|
RegEx to match multiple optional groups of elements (javascript)
By : Robert Cate
Date : March 29 2020, 07:55 AM
To fix the issue you can do I need a regular expression to match the following kind of strings in Javascript: , This regular expression works for me:
|