Results 1 to 7 of 7

Thread: regex question

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default regex question

    Good evening everyone,

    I am trying to match the following pattern

    Java Code:
    blah blah blah \
    blah bloo blum bluk\
    bloo blah blah blom blam\
    Basically anything followed by "\", (optionally) followed by any amount of white space

    The regex i am using is

    Java Code:
     if (myLine.matches(".*\\([\\s]*)(\r)\n$")) {
    
        do something clever
    }
    does not seem to work. In fact error i am getting seems to indicate that there is a problem with it.

    The error is
    Java Code:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 8
    .*\([\s]*)(
    )
    $

    .. everything appears to be matched however ...

    Please review and advise what am i missing and why can't i match this pattern

    Thanks all!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    > anything followed by "\", (optionally) followed by any amount of white space
    (Untested)
    Java Code:
    "[^\\]*\\\\\\s*"
    If that's not it, specify your requirements more clearly.

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    About the regex you tried:
    Java Code:
    ".*\\([\\s]*)(\r)\n$")
    The backslash is a special character used for quoting both in String literals and in regexes. So a single backslash in a regex pattern needs to be represented by four, not two, backslashes in a String literal.

    Your \\( in a String literal becomes \( in the resulting String, thus quoting the left parenthesis and resulting in the error you posted: the right parenthesis is unmatched because the left parenthesis was treated as a literal (

    Apart from all that, if your requirement was correctly expressed, what you tried was horribly complicated ;)

    You can find a good tutorial here:
    Regular Expression Tutorial - Learn How to Use Regular Expressions

    db

  4. #4
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    negative.


    Java Code:
    if (a.matches("[^\\]*\\\\\\s*")) {
    results in

    Java Code:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 9
    [^\]*\\\s*
    Please, what am i missing?

  5. #5
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Well .. let me clarify a few things:

    1. I don't know whether the file i will be looking at will have DOS or UNIX line endings, hence (r) is optional, whereas \n is needed
    2. White space is as well optional. It may or may not be there

    Aso, Not \ is not entirely correct. \ may be there as part of a line, what i need to match is

    ANYTHING followed by \ POSSIBLY followed by White space FOLLOWED by a \n (should work regardless of whether file is saved in DOS or UNIX format)

    ".*\\([\\s]*)(\r)\n$" looks right, but yet it is not somehow
    Last edited by mac; 03-17-2010 at 06:47 AM.

  6. #6
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Getting closer - no errors, but still no match

    Java Code:
    		String a = "blah\\\n";
    		
    		System.out.println(a);
    		
    		if (a.matches(".*\\\\([\\s]*)(\r)\n")) {
    			System.out.print("yes");
    		} else {
    			System.out.print("no");
    		}

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Ok, this works. Darryl - THANK YOU VERY MUCH FOR YOUR HELP


    Java Code:
    		String a = "blah\\\n";
    		
    		String del = System.getProperty("line.separator");
    		
    		if (a.matches(".*\\\\([\\s]*)" + del)) {
    			System.out.print("yes");
    		} else {
    			System.out.print("no");
    		}

Similar Threads

  1. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 06:53 PM
  2. Multiline Regex
    By zriggle in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 05:49 AM
  3. Some help with regex and loop
    By moaxjlou in forum New To Java
    Replies: 21
    Last Post: 11-02-2008, 11:24 PM
  4. Regex Quantifiers Example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 11:44 AM
  5. Regex pattern
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-11-2007, 11:20 AM

Posting Permissions

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