TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Regular Expressions in Java (http://forums.techarena.in/software-development/1298506.htm)

Ash maker 03-02-2010 04:26 AM

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.

GlassFish 03-02-2010 04:28 AM

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"

Ash maker 03-02-2010 04:34 AM

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.

Linux-Us 03-02-2010 04:37 AM

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.

Ash maker 03-02-2010 04:40 AM

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.

HardWeaR 03-02-2010 04:42 AM

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.


All times are GMT +5.5. The time now is 02:49 AM.