tags:

views:

160

answers:

4

I have some document stored as a large String. In the String I have some inline XML tags and I want to get out the words inbetween the tags. The documents may also contain HTML tags, as the documents are often web sites.

Example Document:

"< tr > My name is < b >< PERSON >Bobby< /PERSON >< /b >, I live in the USA."

Current RegEx:

Pattern p = Pattern.compile("<(LOCATION|PERSON|ORGANIZATION)>[\\w[ '\"/\\!%$\\(\\)\\-\\+]]*</(LOCATION|PERSON|ORGANIZATION)>");

Matcher m = p.matcher("I'm <PERSON>Graham Brown</PERSON> I went to the <LOCATION>USA'S</LOCATION>");

while(m.find()){
    System.out.println(m.group());
}

Result = < PERSON >Bobby< /PERSON > < LOCATION >USA< /LOCATION >

This works fine with pretty much most puntuation and grammer, but the Regex should allow any character pattern to be found between the tags. When I try using '.' (any character), as below it returns the whole String.

"< tr > My name is < b >< PERSON >Bobby< /PERSON >< /b >, I live in the USA."

 Pattern p = Pattern.compile("<(LOCATION|PERSON|ORGANIZATION)>.</(LOCATION|PERSON|ORGANIZATION)>");

How do I return any characters between the angular openinng and closing tags?

EDIT: Thanks for your responses. Just and for helping get the correct answer. For clarification I have marked Named Entites using NER. If you are unware of what this is please see some of the papers I have referenced at the bottom.

All I am interested in is getting the text between the three opening and closing tags. There are no other tags and the documents are not XML files and I am not parsing all the HTML tags nor I am I interested in them. All I am interested in is parsing the XML tags that I have created hence I though RegEx would be the simplest way to do so.

Papers to be added later...

+4  A: 

Put a question mark after .*? for non-greedy processes.

Pattern p = Pattern.compile("<(LOCATION|PERSON|ORGANIZATION)>.*?</(LOCATION|PERSON|ORGANIZATION)>");

PS: I am just correcting your regex, but it does not mean it's the solution. Using parsers are always better idea.

S.Mark
Thanks for your help with the RegEx it's not something that I do often and I am not really diving through lots of XML tags. I just needed someway to extract text between 3 different tags and through RegEx would be a quick way to do it as I haven't used parsers much in Java.
Graham
A: 

Please use an XML parser for XML snippets. It's the right tool for your problem.

Edit: And use a HTML sanitizer to preprocess the HTML file. Additionally define a strict XML schema for the XML to ensure the XML structure.

Thomas Jung
You think an XML parser is the right solution for parsing a few sparse XML tags from documents which may contain either raw text or html? I think in this case he's XML tags as a simple form of meta data not as a true treelike structure. For parsing out meta data tags from other documents regexp could be very much the right solution.
Benj
I would only parse the **XML snippets** with the XML parser (see my answer).
Thomas Jung
I don't need to sanitzer the HTML files for NER, see Edit and Comments. Benj has got the right idea.
Graham
I'm puzzled by SO. This http://stackoverflow.com/questions/1802656/java-regex-problem-using-the-all-character-symbol/1802684#1802684 gets + 2 for the same basic content (without a solution). Well, but I'm in good company (http://stackoverflow.com/questions/1802656/java-regex-problem-using-the-all-character-symbol/1802697#1802697 has -2 as well for exactly the some content as the +2 answer). And the accepted answer contains the advise to use a parsers. Is anybody reading the answers anyway. This is all but consistent.
Thomas Jung
+2  A: 

Parsing Html The Cthulhu Way, by Jeff Atwood

Rubens Farias
Thanks for the article on parsing HTML. This is a good article and I will definately being bookmarking it for the future.
Graham
A: 

There's only one answer for this problem: You can't parse HTML with regex

Andreas_D
I am not parsing HTML with regex, see Edit and Comments.
Graham
NER is XML and the references article is true for XHTML as well and therefore true for XML. But good luck with you RegExp anyway. Hope no one else never ever has to maintain that beast. (http://www.jdom.org/)
Andreas_D
+1 Software maintenance what's that?
Thomas Jung
The art of touching a piece of software a second time ;)
Andreas_D