2

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 <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 in a given text.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 2
    HTML? Regular expressions? ``? Where am I? – strager Aug 13 '10 at 07:34
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Paul Tomblin Aug 16 '10 at 17:59
  • possible duplicate of [java regular expression matching](http://stackoverflow.com/questions/3500992/java-regular-expression-matching) –  Aug 17 '10 at 14:35

1 Answers1

6

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
  • 413,195
  • 112
  • 811
  • 826
  • Thanks. That is helpful. I have another question. What pattern do i use to match a number in a given input? – java_geek Aug 13 '10 at 07:49
  • For integers? You could use for instance "\\d+". – aioobe Aug 13 '10 at 08:30
  • Thanks. What if there are multiple matches and i want to replace each one with some pattern?? – java_geek Aug 13 '10 at 08:39
  • 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 Aug 13 '10 at 08:52
  • i did not get what you are trying to convey. My question again Lets say there were multiple matches and i want to replace each match with a different string, how can i do that? – java_geek Aug 16 '10 at 08:06
  • 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 Aug 16 '10 at 11:36
  • 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 tag and replace the ... with appropriate style tag like

    etc based on the size of the font.

    – java_geek Aug 16 '10 at 11:42
  • 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 Aug 16 '10 at 11:59
  • 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 Aug 16 '10 at 12:01
  • 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 Aug 16 '10 at 12:15
  • 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 Aug 16 '10 at 17:09
  • Hi aioobe, I have one final question. I want a regular expression that can match My Name is xyz and
  • My Name is xyz
  • and it should not be greedy – java_geek Aug 17 '10 at 05:19
  • If you need clarifications to this answer, post comments here. If you have new question, then click "Ask Question". – aioobe Aug 17 '10 at 07:46
  • I have created one.. But no responses http://stackoverflow.com/questions/3495819/java-regular-expression-question – java_geek Aug 17 '10 at 08:18
  • Note that you _must_ call m.find() before opening the groups (past hour of my life, good bye!) – chessofnerd Jul 24 '13 at 16:14