Flutter cannot parse this working regex, and doesn't return any error or info.
(?<=id=)[^&]+
However, when I add it into my Flutter app:
print("before");
new RegExp(r'(?<=id=)[^&]+');
print("after");
It doesn't do anything, doesn't return any error. The print("after"); never gets executed. It doesn't completly freeze the app, because it's in async.
Dart compiled for the Web supports lokbehinds, but the current version of native Dart (including Flutter) does not support lookbehinds (source).
In your case, you want to match a string after a specific string. All you need is to declare a capturing group in your pattern and then access that submatch:
RegExp regExp = new RegExp(r"id=([^&]+)");
String s = "http://example.com?id=some.thing.com&other=parameter; http://example.com?id=some.thing.com";
Iterable<Match> matches = regExp.allMatches(s);
for (Match match in matches) {
print(match.group(1));
}
Output:
some.thing.com
some.thing.com
Here, id=([^&]+) matches id= and then the ([^&]+) capturing group #1 matches and captures into Group 1 any one or more chars other than &. Note you may make it safer if you add [?&] before id to only match id and not thisid query param: [?&]id=([^&]+).
I assume this is https://github.com/dart-lang/sdk/issues/34935
Bring Dart's RegExp support in line with JavaScript: lookbehinds, property escapes, and named groups.
Related
I would like to use Regular expression to extract content between brackets, after some specific string and the 1st match.
Example text:
**-n --command PING being applied--:
Wed May 34 7:23:18 2010
[ZZZ_6323] Command [ping] failed with error [[TEZZZGH_IUE] [[EIJERTMMMMIJE_EIEJ] gdyugedyue Service [ABC] is not available in domain [DEF]. Check the content and review diejidjei. Service [ABC] Domain [DEF] ] did not ping back. It might be due to one of the following reasons:
=> Reason1
=> Reason3
=> Reason 4: deijdije djkeoidjeio.
info=4343 day=Mon year=2010*
I would like to extract the string between [] but after string Service and 1st match as Service could appear again later. In this case ABC
Could someone help me?
I am not able to combine these three conditionals.
Thanks
Assuming that you don't care about capturing square brackets inside the [ ] pair, by far the easiest way to do this is to use the following simple regex:
Service (\[[^\]]*\])
and extract only the 1st capturing group from the result using whatever regex functionality you're using. For example, using JS, you would write
string.match(/Service (\[[^\]]*\])/)[1]
to extract the first capturing group.
If you instead want a regex that will only capture the first occurrence, you can exploit the greedy nature of the * quantifier and change the regex to this:
Service (\[[^\]]*\]).*
Service \[([^\]]+)\]
will match Service [anything besides brackets] and capture anything besides brackets in group number 1. Since regex engines work left-to-right, the first match will be the leftmost match.
Test it live on regex101.com.
In PHP, you could do this (code snippet generated by RegexBuddy):
if (preg_match('/Service \[([^\]]+)\]/', $subject, $groups)) {
$result = $groups[1];
} else {
$result = "";
}
The definition of the group name How should I write it? I know that it can be like this: (?) but I dont know how to combine it with this part Service [([^]]+)] in a single way
I am a new user of svlib package in systemverilog environment. Refer to Verilab svlib. I have following sample text , {'PARAMATER': 'lollg_1', 'SPEC_ID': '1G3HSB_1'} and I want to use regex to extract 1G3HSB from this text.
For this reason, I am using the following code snippet but I am getting the whole line instead of only the information.
wordsRe = regex_match(words[i], "\'SPEC_ID\': \'(.*?)\'");
$display("This is the output of Regex: %s", wordsRe.getStrContents())
Can anybody direct me what is going wrong?
The output I am getting : {'PARAMATER': 'lollg_1', 'SPEC_ID': '1G3HSB_1'}
And, I want to get: 1G3HSB_1
It seems you need to get the contents of the first capturing group with getMatchString(1). Also, you need to use a greedy quantifier (lazy ones are not POSIX compliant) and a negated bracket expression - [^']* instead of .*?:
wordsRe = regex_match(words[i], "\'SPEC_ID\': \'([^\']*)\'");
$display("This is the output of Regex: %s", wordsRe.getMatchString(1))
See the User Guide details:
getMatchString(m) is always exactly equivalent to calling the range method on the Str object containing the string that was searched:
range(getMatchStart(m), getMatchLength(m))
Hi i will have URL in following format:
http://www.youtube.com/v/0PsnoiwMrhA
https://www.youtube.com/v/0PsnoiwMrhA
www.youtube.com/v/0PsnoiwMrhA
http://youtube.com/v/0PsnoiwMrhA
youtube.com/v/0PsnoiwMrhA
It all must capture and return a domain name as youtube.
I have tried using
(http://|https://)?(www.)(.?*)(.com|.org|.info|.org|.net|.mobi)
but it showing error as regex parsing nested quantifier.
Please help me out
If you are using a field that you know is in one of these formats, you can retrieve the match from Group 1 using this regex:
^(?:https?://)?(?:www\.)?([^.]+)
In VB.NET:
Dim ResultString As String
Try
ResultString = Regex.Match(SubjectString, "^(?:https?://)?(?:www\.)?([^.]+)", RegexOptions.Multiline).Groups(1).Value
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
(.?*) should be (.*?) - that's the source of your error.
Also, remember to escape the dot unless you want it to match any character.
And since the www. part is optional, you need to add a ? quantifier to that group as well.
You could try the below regex to get the domain name youtube from the above mentioned URL's,
^(?:https?:\/\/)?(?:www\.)?([^.]*)(?=(?:\.com|\.org|\.info|\.net|\.mobi)).*$
DEMO
It ensures that the domain name must be followed by .com or .info or .org or .net or .mobi.
For practice, I decided to build something like a Backbone router. The user only needs to give the regex string like r'^first/second/third/$' and then hook that to a View.
For Example, suppose I have a RegExp like this :
String regexString = r'/api/\w+/\d+/';
RegExp regExp = new RegExp(regexString);
View view = new View(); // a view class i made and suppose that this view is hooked to that url
And a HttRequest point to /api/topic/1/ and that would match that regex, then i can rendered anything hook to that url.
The problem is, from the regex above, how do i know that \w+ and \d+ value is topic and 1.
Care to give me some pointers anyone? Thank you.
You need to put the parts you want to extract into groups so you can extract them from the match. This is achieved by putting a part of the pattern inside parentheses.
// added parentheses around \w+ and \d+ to get separate groups
String regexString = r'/api/(\w+)/(\d+)/'; // not r'/api/\w+/\d+/' !!!
RegExp regExp = new RegExp(regexString);
var matches = regExp.allMatches("/api/topic/3/");
print("${matches.length}"); // => 1 - 1 instance of pattern found in string
var match = matches.elementAt(0); // => extract the first (and only) match
print("${match.group(0)}"); // => /api/topic/3/ - the whole match
print("${match.group(1)}"); // => topic - first matched group
print("${match.group(2)}"); // => 3 - second matched group
however, the given regex would also match "/api/topic/3/ /api/topic/4/" as it is not anchored, and it would have 2 matches (matches.length would be 2) - one for each path, so you might want to use this instead:
String regexString = r'^/api/(\w+)/(\d+)/$';
This ensures that the regex is anchored exactly from beginning to the end of the string, and not just anywhere inside the string.
I have to create a route using regex that matches a URL which does not end with a particular word say 'submit'. For example -
/login/submit ==> does not match
/login/abcsubmit ==> does not match
/abc/xyx => Matches
Use this regex:
((?!(.*?)/\w*submit).*)
like explained in http://backbonejs.org/#Router-route
this.route(/^((?!(.*?)/\w*submit).*)$/, "functionName");
I had tried #Nestenius regex that he provided and it was still matching the first two example urls that you had provided. The reason it was is because the regex was not anchored to the start of the string.
You could still use his regex if you add an ^ tag to the beginning of the regex like so:
^((?!(.*?)/\w*submit).*)
Or you can use this shorter version:
^(?!.*submit).*
Both will match any string that does not contain "submit" in it.