If I search a string for matches to a regex which is the union of two or more sub-regexen, is there any way to determine which sub-regex matches without checking each of them individually?
For example, if I have the code:
var regExp = new RegExp('ab|cd');
var matches = regExp.allMatches('absolutely fabulous');
the search returns two matches - but is there a way for me to know which match corresponds to which sub-regex?
Found an answer thanks to searching for branches.
var regExp = new RegExp('(ab)|(cd)'); //brackets are significant
var matches = regExp.allMatches('absolutely fabulous');
var m1 = match.first;
print(m1.group(1)); // 'ab'
print(m1.group(2)); // null, since second term (cd) not matched here
var m2 = match.last;
print(m2.group(1)); // null, since first pattern not matched here
print(m2.group(2)): // 'ac'
Other useful info at
dart regex matching and get some information from it
Related
I don't found how to add variable in a regex
for example I have a String who is an output of list
MyString ="list:[2020-09-05 12:23, 2020-09-04 12:23]"
I use regex to extract date match = 2020-09-05
But I don't found how to add this match variable in other regex area expression, like that:
RegExp regExp = new RegExp(
r'((Prefix$match)[1-9]\S(,suffix))',);
If your list is filled with those types of elements, I would use
var element = string.splits(' ').first;
Hi I need help in detecting words that are nonsensical like these: Okkkk or alrrriiigghht
I found this block of code:
var string = "alrrright";
var regex = /(\w)\1+/g;
var res = regex.test(string);
alert(res);
It returns false if it detects that there are no characters that repeats more than once and true if there are any. I need to raise the number of repeated characters. How do I do that? Sorry I really suck at regex.
Replace the + with {n-1,}, where n is the number of repeated characters:
var regex = /(\w)\1{2,}/g;
Debuggex Demo
I'm trying to get my Dart web app to: (1) determine if a particular string matches a given regex, and (2) if it does, extract a group/segment out of the string.
Specifically, I want to make sure that a given string is of the following form:
http://myapp.example.com/#<string-of-1-or-more-chars>[?param1=1¶m2=2]
Where <string-of-1-or-more-chars> is just that: any string of 1+ chars, and where the query string ([?param1=1¶m2=2]) is optional.
So:
Decide if the string matches the regex; and if so
Extract the <string-of-1-or-more-chars> group/segment out of the string
Here's my best attempt:
String testURL = "http://myapp.example.com/#fizz?a=1";
String regex = "^http://myapp.example.com/#.+(\?)+\$";
RegExp regexp= new RegExp(regex);
Iterable<Match> matches = regexp.allMatches(regex);
String viewName = null;
if(matches.length == 0) {
// testURL didn't match regex; throw error.
} else {
// It matched, now extract "fizz" from testURL...
viewName = ??? // (ex: matches.group(2)), etc.
}
In the above code, I know I'm using the RegExp API incorrectly (I'm not even using testURL anywhere), and on top of that, I have no clue how to use the RegExp API to extract (in this case) the "fizz" segment/group out of the URL.
The RegExp class comes with a convenience method for a single match:
RegExp regExp = new RegExp(r"^http://myapp.example.com/#([^?]+)");
var match = regExp.firstMatch("http://myapp.example.com/#fizz?a=1");
print(match[1]);
Note: I used anubhava's regular expression (yours was not escaping the ? correctly).
Note2: even though it's not necessary here, it is usually a good idea to use raw-strings for regular expressions since you don't need to escape $ and \ in them. Sometimes using triple-quote raw-strings are convenient too: new RegExp(r"""some'weird"regexp\$""").
Try this regex:
String regex = "^http://myapp.example.com/#([^?]+)";
And then grab: matches.group(1)
String regex = "^http://myapp.example.com/#([^?]+)";
Then:
var match = matches.elementAt(0);
print("${match.group(1)}"); // output : fizz
I want to extract a portion of a string, allowing for the dash character to appear randomly throughout. In my match, I want the dash character occurrences to be included.
Let's say I have a scenario like so:
haystack = "arandomse-que-nce"
needle = "sequence"
and I want to come out on the other end with a string like se-que-nce this this case, what would the regex pattern look like?
I would split the string and then join by -*; for example, in JavaScript:
var needle = "sequence"
var regex = new RegExp(needle.split('').join('-*'))
var result = "arandomse-que-nce".match(regex) // ["se-que-nce"]
var result2 = "a-bad-sequ_ence".match(regex) // null
You could also use a regex to insert -* between each character:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*'))
Both the split/join method and the replace method return 's-*e-*q-*u-*e-*n-*c-*e' for the regex.
If you have characters like * in your string, that have meanings in regular expressions, you may want to escape them, like so:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*')
.replace(/([-\\^$*+?.()|[\]{}])/g, '\\$1'))
Then, if needle was 1+1, for example, it would give you 1-*\+-*1 for the regex.
s-*e-*q-*u-*e-*n-*c-*e-*
The assumes that multiple hyphens in a row are okay.
EDIT: Doorknob's split/join solution is good, but be aware that it only works for character that aren't special characters (*, +, etc.)
I don't know what the specifications are, but if there are special characters, make sure to escape them:
new RegExp(needle.split('').map(function(c) { return '\\' + c; }).join('-*'))
You could try to use:
s-?e-?q-?u-?e-?n-?c-?e
My field is supposed to be in the format of A111-1A1, but my regex allows the very last number to be more than one digit (eg. A111-1A1212341). How do I fix this?
Below is the regex I am currently using.
var validchar = /^[A-Z](([0-9]{3})+\-)[0-9][A-Z][0-9]+$/;
Remove the + at the end of your pattern. That is what allows for more than one numeric at the end.
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
However, your pattern otherwise doesn't look right to do what you say you want. Is that really the exact pattern you are using?
Try this
var validchar = /^[A-Z][0-9]{3}\-[0-9][A-Z][0-9]$/;
Or remove the + from the end of your regex
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
Just remove the final + from your regex:
var validchar = /^[A-Z]([0-9]{3})+\-[0-9][A-Z][0-9]$/;