tags:

views:

67

answers:

4

Hi guys i am trying to figure out a simple regEx from the given text below:

<b>Name:</b> Photomatix.Pro.v4.0.64bit-FOSI<br />

i basically want to output and store only Photomatix.Pro.v4.0.64bit-FOSI i.e. the actual value thats inside the but when i define it like this:

private static final String REG_NAME = "<b>Name:</b>(.*)<br />";

It actually stores and outputs the whole <b>Name:</b> Photomatix.Pro.v4.0.64bit-FOSI<br />

Any ideas on how i can just extract the value given from the above xml text? cheers in advance

+6  A: 

This should work:

  final String REG_NAME = "<b>Name:</b>(.*)<br />";

        String text = "<b>Name:</b> Photomatix.Pro.v4.0.64bit-FOSI<br />";

        Pattern pattern = Pattern.compile(REG_NAME);

        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
mgamer
`group(1)` does the trick, `group(0)` is the complete match, which is equal to the OP's "unexpected" result.
Andreas_D
nice one thanks
jonney
+1 Gah, just beat me to it!
Steve Claridge
edit: how does it group strings together? i would like to understand the concept of grouping.
jonney
@jonney: check it out: http://www.exampledepot.com/egs/java.util.regex/Group.html
mgamer
A: 

(I don't have a Java compiler at hand right now so I cannot verify the answer. Thus this isn't the final answer, but...)

If you really want to do it with regexes, you should take a look at matchers and groups in the Java regex classes.

DerMike
+1  A: 
String r = "/b>(.*)<b";

Pattern p = Pattern.compile( r );
Matcher m = p.matcher( "<b>Name:</b> Photomatix.Pro.v4.0.64bit-FOSI<br />" );

if ( m.find() )
{
  System.out.println( "found: " + m.group(1) );
}
Steve Claridge
You know full well that your answer is incorrect. It will work for a string like "b>Name:</b Photomatix.Pro.v4.0.64bit-FOSI" which is not what joneey expected. My answer is not perfect either!
mgamer
indeed, but I think we gave him enough to work with. We can't know all his possible string inputs anyway.
Steve Claridge
Absolutely Steve! +1
mgamer
A: 

well, i don't think it is such a good idea to parse an html using regexes, you should use some html java parsers. if you want to use regular expression though, you can check some examples here: Regular Expressions

virgium03
i wasnt aware their was any html java parsers.
jonney