Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get regex matched group values

Tags:

java

regex

I have following lines of code

String time = "14:35:59.99"; String timeRegex = "(([01][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])(.([0-9]{1,3}))?"; String hours, minutes, seconds, milliSeconds; Pattern pattern = Pattern.compile(timeRegex); Matcher matcher = pattern.matcher(time); if (matcher.matches()) {     hours = matcher.replaceAll("$1");     minutes = matcher.replaceAll("$4");     seconds = matcher.replaceAll("$5");     milliSeconds = matcher.replaceAll("$7"); } 

I am getting hours, minutes, seconds, and milliSeconds using the matcher.replace method and back references of regex groups. Is there any better method to get value of regex groups. I tried

hours = matcher.group(1); 

but it throws the following exception:

java.lang.IllegalStateException: No match found     at java.util.regex.Matcher.group(Matcher.java:477)     at com.abnamro.cil.test.TimeRegex.main(TimeRegex.java:70) 

Am I missing something here?

like image 300
Bilal Mirza Avatar asked Jul 26 '12 09:07

Bilal Mirza


People also ask

How do I match a group in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

How does regex grouping work?

What is Group in Regex? A group is a part of a regex pattern enclosed in parentheses () metacharacter. We create a group by placing the regex pattern inside the set of parentheses ( and ) . For example, the regular expression (cat) creates a single group containing the letters 'c', 'a', and 't'.

What is capturing group in regex JavaScript?

A part of a pattern can be enclosed in parentheses (...) . This is called a “capturing group”. That has two effects: It allows to get a part of the match as a separate item in the result array.

What does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.


1 Answers

It works fine if you avoid calling matcher.replaceAll. When you call replaceAll it forgets any previous matches.

String time = "14:35:59.99"; String timeRegex = "([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?"; Pattern pattern = Pattern.compile(timeRegex); Matcher matcher = pattern.matcher(time); if (matcher.matches()) {     String hours = matcher.group(1);     String minutes = matcher.group(2);     String seconds = matcher.group(3);     String miliSeconds = matcher.group(4);     System.out.println(hours + ", " + minutes  + ", " + seconds + ", " + miliSeconds); } 

Notice that I've also made a couple of improvements to your regular expression:

  • I've used non-capturing groups (?: ... ) for the groups that you aren't interested in capturing.
  • I've changed . which matches any character to \\. which matches only a dot.

See it working online: ideone

like image 130
Mark Byers Avatar answered Sep 30 '22 14:09

Mark Byers