views:

86

answers:

1

I have a string and a pattern that i want to search for in that string. Now, when i match the pattern in the string, i want to know the string that matches my pattern.

String template = "<P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"12\"> My Name is xyz </FONT></P>";

String pattern = "<FONT.*>";

Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);

How can i find out the string that matches the pattern in my template.

i.e i want to get <FONT FACE=\"Verdana\" SIZE=\"12\"> as the result. Is there any method provided by the regex library in java which can help me?

Update:

What is the regular expression that can match My Name is xyz and

  • My Name is xyz
  • and it should not be greedy in a given text

    +2  A: 

    You can access the matched part of the string through matcher.group() .

    A more elaborate way is to use capturing groups. Put the part you want to "extract" from the matched string within parethesis, and then get hold of the matched part through, for instance matcher.group(1).

    In your particular example, you need to use a reluctant qualifier for the *. Otherwise, the .* will match all the way to the end of the string (since it ends with P>).

    To illustrate both the use of Matcher.group() and Matcher.group(int):

    String template = "<P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"12\"> My Name is xyz </FONT></P>";
    String pattern = "<FONT (.*?)>";
    
    Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(template);
    
    if (m.find()) {
        // prints: <FONT FACE="Verdana" SIZE="12"> My Name is xyz </FONT></P>
        System.out.println(m.group());
    
        // prints: FACE="Verdana" SIZE="12"
        System.out.println(m.group(1));
    }
    
    aioobe
    Thanks. That is helpful. I have another question. What pattern do i use to match a number in a given input?
    java_geek
    For integers? You could use for instance "\\d+".
    aioobe
    Thanks. What if there are multiple matches and i want to replace each one with some pattern??
    java_geek
    You could use String.replaceAll... it actually takes a regexp as first argument. If you need the matched string, simply refer to it as $0.
    aioobe
    i did not get what you are trying to convey. My question againLets say there were multiple matches and i want to replace each match with a different string, how can i do that?
    java_geek
    How are the replacement-strings different from each other? can you give an example? If they are completely unrelated I suggest you do multiple calls to replaceFirst.
    aioobe
    My template string is actually a combination of several of the tags that i have shown above. The size= attribute may have different values in each of these tags. Now, what i want to do is get the size to be used for the particular text from the <font> tag and replace the <Font>...</Font> with appropriate style tag like <p size="12pt;"> etc based on the size of the font.
    java_geek
    import java.util.regex.Matcher;import java.util.regex.Pattern; public class PatternCompileTest1 { public static void main(String[] args) { String template="My Name is Sachin Tendulkar. Sachin is a good boy. Sachin is from India"; String pattern = "Sachin"; Pattern p = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(template); int count = 1; while (m.find()) { String match = m.group(); String replacement = match + count++; template = m.replaceFirst(replacement); System.out.println("template is " + template); } } }
    java_geek
    This is a sample program that i have written but it goes into an infinite loop. I didn't know how to edit, so it looks a bit awkward...
    java_geek
    Sorry, I don't have a solution for you at the moment. (You could process the string and meanwhile construct a new string with the replaced strings.) You should start a new question. This is quite far from the original question.
    aioobe
    I have found a solution.Pattern p = Pattern.compile("cat"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, "dog"); } m.appendTail(sb); System.out.println(sb.toString());
    java_geek
    Well, good for you :)
    aioobe
    Hi aioobe, I have one final question.I want a regular expression that can match <FONT FACE=\"Verdana\" SIZE=\"12\"> My Name is xyz </FONT> and<LI><FONT FACE=\"Verdana\" SIZE=\"12\"> My Name is xyz </FONT></LI> and it should not be greedy
    java_geek
    If you need clarifications to this answer, post comments here. If you have new question, then click "Ask Question".
    aioobe
    I have created one.. But no responseshttp://stackoverflow.com/questions/3495819/java-regular-expression-question
    java_geek