aspose file tools
The moose likes Java in General and the fly likes Pattern & Matcher - how to get any combination? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Pattern & Matcher - how to get any combination? " Watch "Pattern & Matcher - how to get any combination? " New topic
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
    
    1

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
    
    1

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Pattern & Matcher - how to get any combination?
 
Similar Threads
Spliting a string on a second space and not all spaces
Regular Expressions in String.split()
Regarding String Tokenizer
Replace text by HTML links
environment variable referance in Web.xml