Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pattern matching

 
etika ahuja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the pattern does not get matched if the url contains " ? " why so ?

what is the solution to this problem
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "?" has special meaning in regular expressions. You need to escape it by putting a "\" before it. (really two because the \ is a special character in Java and needs escaping itself.)

In the regular expression world, ? means 0 or 1 of the previous character.
 
etika ahuja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.regex.*;

class pattern
{
public static void main(String[] args)
{
String temp="http://railsforum.com/viewtopic.php\\?id=38039";
String par="http://railsforum.com/viewtopic.php\\?id=38039";
Pattern pat= Pattern.compile(par);
Matcher mat= pat.matcher(temp);

if(mat.find())
{
System.out.println("match found");

}
}
}


it is still not be able match the pattern
 
etika ahuja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the answer..
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.regex.*;

class pattern
{
public static void main(String[] args)
{
String temp="http://railsforum.com/viewtopic.php?id=38039";
String par="http://railsforum.com/viewtopic.php\\?id=38039";
Pattern pat= Pattern.compile(par);
Matcher mat= pat.matcher(temp);

if(mat.find())
{
System.out.println("match found");

}
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic