wood burning stoves 2.0*
The moose likes Java in General and the fly likes Combine two Regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Combine two Regular expressions" Watch "Combine two Regular expressions" New topic
Author

Combine two Regular expressions

Vikram Ramaswamy
Greenhorn

Joined: Mar 21, 2009
Posts: 9
Hi,
String s= "[12/22/09 22:23:57:357 MST] Some junk text that I am not interested in or not required. There is/are 10 more junk text"

From the above string, I would like to extract the time (stuff between []) and the number (10 in this case) that would look like this;
[12/22/09 22:23:57:357 MST] 10

I tried using the RE \[.*\]|\s\d\d?\s, but the problem is I get [12/22/09 22:23:57:357 MST] when I use \[.*\] and 10 when I use \s\d\d?\s, but when I combine them with a "|", I get only the date. I tried searching forums in vain, but am stuck. Any help is appreciated.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Well, how did you combine them? Just "with a "|"", doesn't tell us anything.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vikram Ramaswamy
Greenhorn

Joined: Mar 21, 2009
Posts: 9
I apologise if it wasnt clear enough...The Regular Expression I tried was \[.*\]|\s\d\d?\s hoping to extract [12/22/09 22:23:57:357 MST] 10 from string "[12/22/09 22:23:57:357 MST] Some junk text that I am not interested in or not required. There is/are 10 more junk text", but it isn't working as expected. It extracts only [12/22/09 22:23:57:357 MST] (including the square brackets), but not the number (10).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Oh I see. You want the regex engine to extract two different parts from the string, and merge it as one result for you.... Nope. Can't be done. Regex doesn't work that way.

The best you can do is have a regex with groups -- one for each component. And then form the result from the match group (ie. create the merged string yourself using a string concat).

Henry
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Posts: 35
The simple explanation is to create a regular expression that will match with the entire line of text and put parentheses around what you want. This is called grouping. Read about java.util.regex.Matcher and look at retrieving groups.



-- Mazer
 
Don't get me started about those stupid light bulbs.
 
subject: Combine two Regular expressions
 
Similar Threads
JSF Example Error
java.lang.ClassCastException: javax.naming.Reference incompatible with QueueConnectio
Apache CXF and Properties file
Stale Connection Exception
no suitable driver