I have this regex
regex = ~/\"([^"]*)\"/
so Im looking for all text between quotes
now i have the following string
options = 'a:2:{s:10:"Print Type";s:8:"New Book";s:8:"Template";s:9:"See Notes";}'
however doing
regex.matcher(options).matches() => false
should this not be true, and shouldn't I have 4 groups
The matcher() method tries to match the entire string with the regex which fails.
See this tutorial for more info.
I don't know Groovy, but it looks like the following should work:
def mymatch = 'a:2:{s:10:"Print Type";s:8:"New Book";s:8:"Template";s:9:"See Notes";}' =~ /"([^"]*)"/
Now mymatch.each { println it[1] } should print all the matches.
Related
Im a fresher to RegEx.
I want to get all Syllables out of my String using this RegEx:
/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi
And I implemented it in Dart like this:
void main() {
String test = 'hairspray';
final RegExp syllableRegex = RegExp("/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi");
print(test.split(syllableRegex));
}
The Problem:
Im getting the the word in the List not being splitted.
What do I need to change to get the Words divided as List.
I tested the RegEx on regex101 and it shows up to Matches.
But when Im using it in Dart with firstMatch I get null
You need to
Use a mere string pattern without regex delimiters in Dart as a regex pattern
Flags are not used, i is implemented as a caseSensitive option to RegExp and g is implemented as a RegExp#allMatches method
You need to match and extract, not split with your pattern.
You can use
String test = 'hairspray';
final RegExp syllableRegex = RegExp(r"[^aeiouy]*[aeiouy]+(?:[^aeiouy]*$|[^aeiouy](?=[^aeiouy]))?",
caseSensitive: true);
for (Match match in syllableRegex.allMatches(test)) {
print(match.group(0));
}
Output:
hair
spray
I am using a regex regular expression like so:
#Test
fun timePatternFromInstantIsValid() {
val instantOfSometimeEarlier = Instant.now().minus(Duration.ofMinutes((1..3).random().toLong()))
val timeOfEvent = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneId.of("UTC")).format(instantOfSometimeEarlier)
val regex = "(\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))T(?:(?:([01]?\\d|2[0-3]):)?([0-5]?\\d):)?([0-5]?\\d)"
val acceptedDatePattern: Pattern = Pattern.compile(regex)
val matcher: Matcher = microsoftAcceptedDatePattern.matcher(timeOfEvent)
val isMatchToAcceptedDatePattern: Boolean = matcher.matches()
print(isMatchToAcceptedDatePattern)
}
isMatchToAcceptedDatePattern for some reason is returning false which probably indicates something is wrong in my regex BUT, when checking it on multiple regex websites I'm getting a valid match. any ideas why?
try it yourself:
https://www.regextester.com/ or here:
https://regex101.com/
my regex - raw (as in the websites):
(\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))T(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)
pattern example returned like this (it gets returned without the " ' " near the "T"):
2021-04-01T11:12:51 (when I debug this is what I get)
date pattern:
yyyy-MM-ddTHH:mm:ss
could someone inlight me please?
You use matcher.matches() which is like pre- and appending ^ resp. $ to your regex. Such a regex won't work.
Instead you should either:
use matcher.find() which returns true if a match could be found.
prepend \d{2} to your regex and still use matcher.matches(): Demo
I want to split up a camelCase string with spaces.
"ClassicalMusicArtist" -> "Classical Music Artist"
I should be able to do this by replacing "/([a-z](?=[A-Z]))/g" with "$1 " (regex101).
But my regex is not getting any matches:
val regex = "/([a-z](?=[A-Z]))/g".r
val s = "ClassicalMusicArtist"
regex.replaceAllIn(s, "$1 ") // -> Returns "ClassicalMusicArtist"
regex.findFirstIn(s) // -> Returns None
What am I doing wrong? I used the regex in another language with success and can't figure out why I am not getting any matches.
Ok I figured it out.
In scala the regex has to be val regex = "([a-z](?=[A-Z]))".r without the leading / and the modifier.
I have a text of the form:
können {konnte, gekonnt} Verb
And I want to get a match for all words in it that are not in parenthesis. That means:
können = 1st match, Verb = 2nd match
Unfortunately I still don't get the knock of regular expression. There is a lot of testing possibility but not much help for creation unless you want to read a book.
I will use them in Java or Python.
In Python you could do this:
import re
regex = re.compile(r'(?:\{.*?\})?([^{}]+)', re.UNICODE)
print 'Matches: %r' % regex.findall(u'können {konnte, gekonnt} Verb')
Result:
Matches: [u'können ', u' Verb']
Although I would recommend simply replacing everything between { and } like so:
import re
regex = re.compile(r'\{.*?\}', re.UNICODE)
print 'Output string: %r' % regex.sub('', u'können {konnte, gekonnt} Verb')
Result:
Output string: u'können Verb'
A regex SPLIT using this pattern will do the job:
(\s+|\s*{[^}]*\}\s*)
and ignore any empty value.
I am having issues generating a regular expression that works in Groovy.
My aim is: given a string like:
/products/prodA/index-tab2.html
to get a match that returns a match if the string after the last / contains "-tab"n".html"
My initial attempt is with
([^\/]+)(?<=-tab[0-9]\.html$)
which I tested here http://gskinner.com/RegExr/ against the following test data:
/products/prodA/index-tab2.html
/products/prodA/index.html
/products-tab2/prodA/index-tab2.html
and got matches on "index-tab2.html" - so far so good (or so I thought).
Next step is to put this into Groovy:
log.info("KPF: pageName is ${pageName} ")
def matcher = pageName =~ /([^\/]+)(?<=tab[0-9]\.html$)/
if (matcher.matches()) {
log.debugEnabled && log.debug("KPF: Filename has tab = $filename")
} else {
log.debugEnabled && log.debug("KPF: Filename does not have tab")
}
however when I test the code with the input
/products/prodA/index-tab2.html
(there is no trailing space - verified - but left out of this example)
I get the following logged:
2013-07-02~12:51:10 INFO (xxx.site.controllers.PageController # line 35) KPF: pageName is /products/prodA/index-tab2.html (xxx)
2013-07-02~12:51:10 DEBUG (xxx.site.controllers.PageController # line 44) KPF: Filename does not have tab (xxx)
So which regex is "wrong" and how do I get the match I need?
matcher.matches() requires that the whole string match the regular expression, so it will only return true if pageName contains no slashes at all. You probably want to use find() instead of matches(), which returns true if a match is found anywhere in the string.
log.info("KPF: pageName is ${pageName} ")
def matcher = pageName =~ /([^\/]+)(?<=tab[0-9]\.html$)/
if (matcher.find()) {
log.debugEnabled && log.debug("KPF: Filename has tab = ${matcher.group(1)}")
} else {
log.debugEnabled && log.debug("KPF: Filename does not have tab")
}
Or indeed just if(matcher) as a Matcher coerces to boolean in Groovy by calling find(). This is done to support syntax like
if(pageName =~ /..../)
but in your case you need a reference to the actual Matcher in order to extract the parenthesised group.