• Post Reply Bookmark Topic Watch Topic
  • New Topic

java regular expression  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I would like to do a string matching with case insensitivity.

Line = "<tiTLE>MyWebPage</Title>"
String input "title"

I need to output this Line if it has variable input in it, irrespective of case.
I should not do toUpperCase or toLowerCase on the Line.

Line.matches(???) what should i use.

I was trying like this. but did not get it
i dont know how to keep the correct regular expression.
may be we can just use String.matches(regex)

import java.util.regex.*;
public class Test {

public static void main(String args[])
{
String Line = "<tiTLE>MyWebPage</Title>";
String input = "title";
Pattern pattern = Pattern.compile(Line, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
boolean matchFound = matcher.matches();
if (matchFound)
System.out.println(Line);

}
}

thanks
 
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, there's like 3 separate things wrong with your solution. I'm guessing you copied this code from the web without really understanding it, as it stands much of that solution doesn't really apply to you.

Here's a short list of the issues:
  • You reversed your input and line of text. The Matcher should be built based on the regular expression, not a specific instance of the string. For example, I build a matcher on "match all strings that contain the word title" in them. So where you call compile, should have a regular expression equivalent to that, not the real string your matching on.
  • Second, there is nothing wrong with your case insensitivity matching, doing it with the CASE_INSENSITIVE flag is perfectly fine. You can also use \p{Upper} and \p{Lower} for additional features in your regular expression.
  • Third, you should being using matcher.find() not matcher.matches(). You only use the latter to "match the entire input sequence against the pattern". Read the API for more info.

  •  
    Rayudu addagarla
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Scott Selikoff
    That was nice explanation!
    I got it working.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!