Results 1 to 3 of 3
  1. #1
    Fhernd is offline Member
    Join Date
    Oct 2011
    Location
    Bogotá Colombia
    Posts
    5
    Rep Power
    0

    Default Regular expression not accepted in Java

    Hola,
    I´m trying to compile the following Java RegEx expression:

    Java Code:
    \<the +the\>
    As you can see, it is very simple. In egrep (UNIX utility) it works correctly, and in Notepad++, too.

    This is the code that I have wrote to test it:

    Java Code:
    public class Backreferencing {
    	public static void main( String[] args ) {
    		try {
    			BufferedReader br = new BufferedReader( new FileReader( "regex-files/ch01/ex12.backreferencing.txt" ) );
    			
    			String currentLine = null;
    			
    			Pattern p = Pattern.compile( "\\<the +the\\>" );
    			
    			while( ( currentLine = br.readLine() ) != null){
    				
    				Matcher m = p.matcher(currentLine);
    				
    				while( m.find() ){
    					System.out.println( currentLine );
    				}
    			}
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    ex12.backreferencing.txt file contents:

    She likes to eat chocolate. But the the chocolate is not good in exceeds.

    Some idea or suggestion?

    Thanks in advance.
    Saludos,

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Regular expression not accepted in Java

    did you read Pattern (Java Platform SE 6) ?
    There is no \< or \> (start and end of word) in java! for word boundaries in regex in java you can use \b -> "\\bthe +the\\b"

  3. #3
    Fhernd is offline Member
    Join Date
    Oct 2011
    Location
    Bogotá Colombia
    Posts
    5
    Rep Power
    0

    Default Re: Regular expression not accepted in Java

    Quote Originally Posted by eRaaaa View Post
    did you read Pattern (Java Platform SE 6) ?
    There is no \< or \> (start and end of word) in java! for word boundaries in regex in java you can use \b -> "\\bthe +the\\b"
    You said the key word (start and end of word). Thanks for it.

    Sorry, but I am just starting with RegEx.

    Thanks a lot for your help, eRaaaa!

Similar Threads

  1. Java Help Regular Expression
    By geforce in forum New To Java
    Replies: 13
    Last Post: 01-28-2012, 08:29 AM
  2. URGENT HELP : Java Regular Expression
    By asheshrocky in forum Advanced Java
    Replies: 3
    Last Post: 12-07-2011, 02:52 PM
  3. Java regular expression optimization - help needed
    By dpkcv in forum Advanced Java
    Replies: 0
    Last Post: 09-30-2011, 08:42 AM
  4. Java Regular expression ?
    By sidharth in forum Advanced Java
    Replies: 12
    Last Post: 11-14-2009, 11:09 AM
  5. Java Regular Expression help
    By royalibrahim in forum Advanced Java
    Replies: 11
    Last Post: 11-12-2009, 01:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •