I have to process some text that ends in a date, and I'm trying to extract the individual parts using Kotlin RegEx. I can get this to work with a space
, but not with a word boundary \b
.
Working Code:
val name = "Prefix Text Sept 7 2007"
var regex = Regex("""(?<prefix>.*?)\b(?<month>Sept|September) (?<day>\d{1,2}) (?<year>\d{2,4})""")
var matched = regex.matchEntire(name)
println("Prefix: ${matched!!.groups["prefix"]?.value}")
println("Month: ${matched!!.groups["month"]?.value}")
println("Day: ${matched!!.groups["day"]?.value}")
println("Year: ${matched!!.groups["year"]?.value}")
Expected Output:
Prefix: Prefix Text
Month: Sept
Day: 7
Year: 2007
If I replace the second line with:
var regex = Regex("""(?<prefix>.*?)\b(?<month>Sept|September)\b(?<day>\d{1,2})\b(?<year>\d{2,4})""")
I don't get a match. This just replaces the space
before the day and the year with a \b
. I would like to understand why this second attempt does not match.