regex woes

K

kaeli

Hey all,

I'm rather new to the java regular expressions package. I must be doing
something wrong. The following code outputs false, false, and no match
exception. Why? It should match (m.group() expected to return <title>
this is a title</title>). TIA

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class testPatternMatching extends Object
{
public static void main(String [] args)
{
String pageContent = "some text blah <title>this is a title
</title> more text blah";
String title = null;
try
{
Pattern p = Pattern.compile("<title>.*</title>",
Pattern.DOTALL);
Matcher m = p.matcher(pageContent);
System.out.println(m.lookingAt());
System.out.println(m.matches());
title = m.group();
System.out.println(title);
}
catch (Exception e)
{
System.out.println("Exception occured:");
System.out.println(e.getMessage());
}
}
};

--
--
~kaeli~
I love God.
It's His fanclub that I can't stand.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Martin Honnen

kaeli wrote:

I'm rather new to the java regular expressions package. I must be doing
something wrong. The following code outputs false, false, and no match
exception. Why? It should match (m.group() expected to return <title>
this is a title</title>). TIA

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class testPatternMatching extends Object
{
public static void main(String [] args)
{
String pageContent = "some text blah <title>this is a title
</title> more text blah";
String title = null;
try
{
Pattern p = Pattern.compile("<title>.*</title>",
Pattern.DOTALL);
Matcher m = p.matcher(pageContent);

Try

if (m.find()) {
System.out.println("Matched " + m.group());
}

here, that is what you are looking for I think.
System.out.println(m.lookingAt());
System.out.println(m.matches());

Both these try to match from the start of the input string and therefore
return false.
 
S

Stanimir Stamenkov

/kaeli/:
I'm rather new to the java regular expressions package. I must be doing
something wrong. The following code outputs false, false, and no match
exception. Why? It should match (m.group() expected to return <title>
this is a title</title>). TIA

String pageContent =
"some text blah <title>this is a title</title> more text blah";
Pattern p = Pattern.compile("<title>.*</title>", Pattern.DOTALL);
Matcher m = p.matcher(pageContent);
System.out.println(m.lookingAt());
System.out.println(m.matches());

From the Matcher class documentation:

* The 'matches' method attempts to match the entire input
sequence against the pattern.
* The 'lookingAt' method attempts to match the input sequence,
starting at the beginning, against the pattern.
* The 'find' method scans the input sequence looking for the
next subsequence that matches the pattern.

So you need the 'find' method, after all:

m.find();
 
K

kaeli

Try

if (m.find()) {
System.out.println("Matched " + m.group());
}

here, that is what you are looking for I think.

Thanks!

This worked great.

Pattern p = Pattern.compile("<title>.*</title>",
Pattern.DOTALL);
Matcher m = p.matcher(pageContent);
System.out.println(m.lookingAt());
System.out.println(m.matches());
title = m.find()?m.group():"";

System.out.println(title);

--
--
~kaeli~
Contrary to popular opinion, the plural of 'anecdote' is
not 'fact'.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top