Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 03-02-2010
Member
 
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"
Reply With Quote
  #3  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #6  
Old 03-02-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 03:51 AM.