| Author |
Pattern & Matcher - how to get any combination?
|
Adrian Burlington
Ranch Hand
Joined: Jun 16, 2009
Posts: 75
|
|
Hi all,
I have a possible outcome of a string such as this:
<Data1>bla bla</Data1>
<Data1>bla bla, bla bla</Data1>
<Data1>bla bla 7765</Data1>
<Data1>bla bla 77777 bla 888999,;_0 bla </Data1>
Using Pattern and Matcher, I'm trying to get the data (any data) inside the tags.
I have:
Thanks!
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
Hi Adrian. You might want to use a capturing group. You could do it with the following generalized regex:This will match input beginning with a <> opening tag, followed by any number of characters, followed by a closing tag that has the same name as the opening tag.
If you find this match, you can use matcher.group(1) to find the name of the tag, and if it equals "Data1", you can use matcher.group(2) to get the content of the element.
Here is some example code:
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
Note that this will not work if you have nested elements with the same name. If this is the case, you will have to use more than just a regex to solve your problem. The example regex I gave also does not take into account white space, and other special conditions.
Take a good look at the description of the Pattern class. If you have questions, don't hesitate to ask.
|
 |
 |
|
|
subject: Pattern & Matcher - how to get any combination?
|
|
|