Reputation: 2027
I am working with regex in java. I have a file which contains the following pattern followed and preceded by other text. The pattern:
Bombay Garden 5995 Mowry Ave Newark, CA (510) 744-6945
Bombay Garden 172 E. 3rd Ave. San Mateo, CA (650) 548-9966
Bombay Garden 3701 El Camino Real Santa Clara, CA (408) 241-5150
I have the following regex that matches each line. The regex:
(.*?)(\d{1,4})(\s*\w*)*(\w+),(\s*)(CA|AZ|NY)(\s*)(\(?[1-9]\d{2}\)?\s*\d{3}-?\d{4})
This matches one line, but i want to extract the street name, state and the phone number for each of the three branches using Java.
Can anyone help me with this?
Upvotes: 0
Views: 152
Reputation: 14699
You extract the contents of a capturing group by using group()
of the Matcher
object, and passing it a group number, ie:
Matcher m = Pattern.compile(yourRegex).matcher(yourString)
while (m.find())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
String state = m.group(6);
...
}
Group 0 is the entire pattern. Group 1 is your first capturing group, ie in your case m.group(6)
will return the state.
Upvotes: 1