Results 1 to 6 of 6

Thread: Regular Expressions in Java

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Regular Expressions in Java

    Hello,
    I want to recover all the string contained in a java program and even those who may be compose let me explain:
    Code:
    System.out.println ( "test" + "eesf" + "+ fdfdgd") and here ( "test" + "eesf" + "+ fdfdgd)
    And
    Code:
    System.out.println ( "firstName:" + name.toString () + "lastName:" + new Object ()), and in this case ( "firstName:" + name.toString () + "lastName:" + new Object ())
    I define a regular expression:
    Code:
    Matcher match = parser.find (line, "((\ "(.*) \ "(.*))+( \ \) | \ \) \ \ .)(.*))");
    Basically I look at the departure presence of a quotation mark followed dune following character finally another quote. My question is how are regular expression handled in java. Any help will be appreciated.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Regular Expressions in Java

    Hello,
    The problem seems to be that you use * that will consume as much character as possible so that the regexp is valid. Should you use *? was the place that consumes minimum possible to make valid regexp.
    For example for the string: foo "foo" "foo",
    - The regexp ".*" found "test" "foo"
    - The regexp ".*?" found "test" and "foo"

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Regular Expressions in Java

    Hello,
    So I turn my expression to obtain:
    parser.find = match (line, "(\ "(.*) \ "(.*?))+( \ \). | \ \)");
    But I have an error now
    ("class"). append ((String) e.getKey ()). append ( "found in the following places.
    Normally the result is expected.
    ("class") and then ( "Found in the following places.
    I am totally confused here, any explanation will be appreciated. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Regular Expressions in Java

    Hello,
    Carefully reread my previous post is the explanation. You use a quote .* between everything to the last quote. I think you should try this expression. This will help you identify your mistake.
    Code:
    parser.find = match (line, "(\ "(.*?) \ "(.*?))+( \ \). | \ \)");
    If you have any more problem then do post back and we will try to solve your queries.

  5. #5
    Join Date
    Dec 2009
    Posts
    204

    Re: Regular Expressions in Java

    Hello,
    Thanks for your replies. I have used it in this way
    Code:
    parser.find = match (line, "(\ "(.*?) \ "(.*?))+( \ \). | \ \ );)");
    But I have a problem in the same place:
    Code:
    For the phrase: ( "firstName:" + name.toString () + "lastName:" + new Object ());
    the expected result is
    Code:
    firstName: "+ name.toString () +" lastName: "+ new Object ());
    And it just gives me:
    Code:
    "firstName:" + name.toString ()
    Any help on this.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Regular Expressions in Java

    Hello,
    Your problem is that your second (.*?) is reluctant, it will try to take as little as possible and expand if necessary. So it vacommencer not taken in by the closing parenthesis of toString (). And without taking it as we get a match, it stops there. As regular expressions are not recursive in their definition, I do not think it is possible to use them for what you want to do. It would be easier to make parsing by hand line by managing yourself swinging the swing brackets.

Similar Threads

  1. What are an expressions used in python?
    By Atsushi in forum Software Development
    Replies: 4
    Last Post: 31-12-2010, 12:46 AM
  2. What are an Expressions in PHP?
    By michaels in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 05:44 AM
  3. Regular Expressions on the Contents of a File
    By samualres in forum Software Development
    Replies: 4
    Last Post: 19-02-2010, 10:30 PM
  4. Question on regular expressions
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 04:21 AM
  5. Regular expressions for unicode
    By Solaris in forum Software Development
    Replies: 4
    Last Post: 15-12-2009, 12:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,279,507.34550 seconds with 17 queries