Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Java Reg expr

 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this (it also allow zero to many spaces around '=':
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to post the console output of the code I provided:

str = (if varA ="1" && varB= "2" && varC = "3")
replStr = (if varA.equals("1") && varB.equals("2") && varC.equals("3"))
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldn't it be:

and
instead of
 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need to find a way to convert an expression like this
(if varA = "1" && varB = "2" )

into Java-like sintax:

(if varA.equals("1") && varB.equals("2") )

I'd like to use Java 1.4 Api to build a Reg Expression.

I found this solution on a Unix newsgroup
sed 's/ = \("[^"]*"\)/.equals(\1)/g'

but unfortunately it doesn't work with Java String method
replaceAll(regExpr, regExpr)

Can anybody help me to find a way to do it?
How can I do it ?
Thanks a lot
Francesco
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jean-Francois Briere:
Try this (it also allow zero to many spaces around '=':


Note that this doesn't work correctly for a String like "if (var=\"\\\"\")".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
shouldn't it be:


Well, yes - but it doesn't make a difference for the regexp...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full java code you tried?
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then this:

Should produce this output:

str = if (varA ="1" && varB= "2" && varC = "3" && varD = "" && varE="Hello \"World\"" )
replStr = if (varA.equals("1") && varB.equals("2") && varC.equals("3") && varD.equals("") && varE.equals("Hello \"World\"") )
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried, but

"\\s*=\\s*(\"(\\\\\"|[^\"])*\")"

should do it, too, shouldn't it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic