Since you say "aba"-style repetition doesn't count, back-references should make this simple:
(.)\1+
Would find sequences of characters. Try it out:
java.util.regex.Pattern.compile("(.)\\1+").matcher("b").find(); // false
java.util.regex.Pattern.compile("(.)\\1+").matcher("bbb").find(); // true
Answer from vanza on Stack OverflowSince you say "aba"-style repetition doesn't count, back-references should make this simple:
(.)\1+
Would find sequences of characters. Try it out:
java.util.regex.Pattern.compile("(.)\\1+").matcher("b").find(); // false
java.util.regex.Pattern.compile("(.)\\1+").matcher("bbb").find(); // true
If you're checking anagrams maybe a different algorithm would be better.
If you sort your strings (both the original and the candidate), checking for anagrams can be done with a string comparison.
Can't seem to match more than one occurrence of a substring
Extract a single value from multiple occurrences matched by regex
Regular expressions: How can I match multiple groups in any order multiple times?
regex - How to match multiple occurrences of a substring - Stack Overflow
the regex does not find painpainpain but only pain
Let's say I have the following list of strings that I want to match:
['ab', 'cde', 'abb', 'efbc']
Is there a way to write a single RE that will match any string that contains any amount of these strings in any order?
So for example, all of these would be valid matches:
abcdeabb ababbefbc efbccdeab cdecdeabefbcab ababababab
If you don't feel like working it out, I'd appreciate any general guidance as well, such as what I need to look into. Perhaps look-ahead and/or look-behind? I'm not familiar with those.
Thanks!
Adding /g isn't enough if you wish to match multiple occurrences of a substring. If that's the case, reluctant quantifiers may be used as described herein.
Given the string:
<div><p>£20<span class="abc" /><span class="def">56</span></p></div>
You will arrive at the text you wanted using:
\d+.*>\d+
But given the same string repeated two times:
<div><p>£20<span class="abc" /><span class="def">56</span></p></div><div><p>£20<span class="abc" /><span class="def">56</span></p></div>
You will not find the target selection multiple times. You'll only find it once due to the greedy nature of .*. To make .* non-greedy, or reluctant, simply add a ? after the * and you will arrive at:
\d+.*?>\d+
Which will find both occurrences of the substring you asked for as shown here.
To match multiple times use to need use the global option
str.match(/your_expression_here/g)
^
Most regular expression engines expose some way to make your expression global. This means that your expression will applied multiple times. This global flag is usually denoted with the /g marker at the end of your expression. This is your regular expression without the /g flag, while this is what happens when you apply said flag.
Different languages expose such functionality differently, in C# for instance, this is done through the Regex.Matches syntax. In Java, you use while(matcher.find()), which keeps providing sub strings which match the pattern provided.
EDIT: I am not a Python person, but judging from the example available here, you could do something like so:
it = re.finditer(r"[A-Z]{2}", "HeLLo woRLd HOw are YoU")
for match in it:
print "'{g}' was found between the indices {s}".format(g=match.group(), s=match.span())
You can not have multiple groups in this case, but you can have multiple matches. Add the global flag to your regex and use a method to match the regex.
For javscript, it would be /[A-Z]{2}/g.
The method most probably returns an Array of matches, and you can use index to access them.
This seems to work fine:
a = '''MS17010314 MS00030208 IL00171198 IH09850115 IH99400409 IH99410409
IL01771010 IL01791002 IL01930907 IL02360907 CM00010904 IH09520115
MS00201285 MS19050708 MS00370489 MS19011285T'''
import re
patterns = ['[A-Z]{2}[0-9]{8,9}[A-Z]{1}','[A-Z]{2}[0-9]{8,9}']
pattern = '({})'.format(')|('.join(patterns))
matches = re.findall(pattern, a)
print([match for sub in matches for match in sub if match])
#['MS17010314', 'MS00030208', 'IL00171198', 'IH09850115', 'IH99400409',
# 'IH99410409', 'IL01771010', 'IL01791002', 'IL01930907', 'IL02360907',
# 'CM00010904', 'IH09520115', 'MS00201285', 'MS19050708', 'MS00370489',
# 'MS19011285T']
I've added a way to combine all patterns.
i tried using python and the following code worked
import re
s='''MS17010314 MS00030208 IL00171198 IH09850115 IH99400409 IH99410409
IL01771010 IL01791002 IL01930907 IL02360907 CM00010904 IH09520115
MS00201285 MS19050708 MS00370489 MS19011285T'''
lst_of_regex = [a,b]
pattern = '|'.join(lst_of_regex)
print(re.findall(pattern,s))