REgular Expressions

P

Pimousse

Hello,

I made a quick search on this forum but i didn't find what i wanted
(maybe i'm a bad seeker ...)

I'm currently trying to replace some "tokens" in a string.
My string looks like that :

"cos(x2)+ln(x1)+exp(x2)+3*sqrt(x3)+1/ln(x2)+3.1415+exp(ln(x5))+arccos(sin(x4))+cos(x1)"

I want to replace cos by Math.cos, ln by Math.log, arccos by Math.acos
etc etc ...So my final aim would be :

"Math.cos(x2)+Math.log(x1)+Math.exp(x2)+3*Math.sqrt(x3)+1/Math.log(x2)+3.1415+Math.exp(Math.log(x5))+Math.acos(Math.sin(x4))+Math.cos(x1)"

I tried to successivily apply a replaceAll() method on my string, with
different replacement strings.
I had a problem with cos and arccos, as with sin and arcsin or tan and
arctan ... indeed, if i make something like :

myStr = myStr.replaceAll("cos\\(","Math.cos(");

Then I have a problem because it replaces arccos( by arcMath.cos( ...
And I don't want that ! :)
The same problem occurs if I replace first arccos then cos .... the
mistake is just different ;)

So I decided to do it another way, using a pattern and a matcher ...
But I don't succeed in finding the good pattern !
Indeed, I'm trying to find a pattern that matches "cos(" but doesn't
match "arccos".

Can anyone help me with this problem ? Have a link ? Thanks.

@++
Pimousse

PS : I've already read
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://java.sun.com/developer/JDCTechTips/2002/tt1008.html#1
http://java.sun.com/docs/books/tutorial/extra/regex/index.html
So pleased don't give me these links to read ;)
 
C

Cid

Can anyone help me with this problem ? Have a link ? Thanks.

Try a pattern like "\\bcos\\b". \b matches word boundries and should
pick up the 'cos' in "cos(" but not "arccos(",
 
T

Thomas Schodt

Pimousse said:
(maybe i'm a bad seeker ...)
lol

Indeed, I'm trying to find a pattern that matches "cos(" but doesn't
match "arccos".

You are probably looking for the word boundary "\\b".

class Trig {
static final private String fm =

"cos(x2)+ln(x1)+exp(x2)+3*sqrt(x3)+1/ln(x2)+3.1415+exp(ln(x5))+arccos(sin(x4))+cos(x1)";
static final private String to =

"Math.cos(x2)+Math.log(x1)+Math.exp(x2)+3*Math.sqrt(x3)+1/Math.log(x2)+3.1415+Math.exp(Math.log(x5))+Math.acos(Math.sin(x4))+Math.cos(x1)";

public static void main(String[] arg) {
String s = fm;
System.out.println(fm);
s = s.replaceAll("(3\\.14\\d*)","Math.PI");
s = s.replaceAll("(ln)","Math.log");
s = s.replaceAll("(exp|sqrt)","Math.$1");
s = s.replaceAll("(\\b)(cos|sin|tan)","Math.$2");
s = s.replaceAll("(arc)(cos|sin|tan)","Math.a$2");
System.out.println(s);
System.out.println(to);
}
}
 
T

Thomas Weidenfeller

Pimousse said:
I tried to successivily apply a replaceAll() method on my string, with
different replacement strings.
I had a problem with cos and arccos, as with sin and arcsin or tan and
arctan ... indeed, if i make something like :

myStr = myStr.replaceAll("cos\\(","Math.cos(");

Then I have a problem because it replaces arccos( by arcMath.cos( ...
And I don't want that ! :)
The same problem occurs if I replace first arccos then cos .... the
mistake is just different ;)

The most simple "solution" (read: hack) would be to have a

myStr = myStr.replaceAll("arcMath.\\.cos\\(","Math.arccos(");

You get the idea? :)
So I decided to do it another way, using a pattern and a matcher ...
But I don't succeed in finding the good pattern !
Indeed, I'm trying to find a pattern that matches "cos(" but doesn't
match "arccos".

Can anyone help me with this problem ? Have a link ? Thanks.

Without checking it, and without providing the proper escapes to get
this into a Java String, something like replacing

\b(sin|cos|tan|<more keywords here>)\b

with
Math.\n

should do.

Or you could move to Java 1.5. where you can write cos(x) instead of
Math.cos(x) using static imports if I am not mistaken.

/Thomas
 
C

Cid

Try a pattern like "\\bcos\\b". \b matches word boundries and should
pick up the 'cos' in "cos(" but not "arccos(",

The String replaceAll method uses regex too so you can do this
directly from there.


String text = "cos(A) + arccos(b) + sin(b) + cos(c)" ;
System.out.println( text.replaceAll("\\bcos\\b","Math.cos") ) ;
---------- output ----
Math.cos(A) + arccos(b) + sin(b) + Math.cos(c)
 
P

Pimousse

Cid a écrit :
Try a pattern like "\\bcos\\b". \b matches word boundries and should
pick up the 'cos' in "cos(" but not "arccos(",
seems it works like a charm ! I didn't really understand the usefullness
of boundaries by the past ... now I know ! :)
thanks for all !

@++
 
A

Alan Moore

Hello,

I made a quick search on this forum but i didn't find what i wanted
(maybe i'm a bad seeker ...)

I'm currently trying to replace some "tokens" in a string.
My string looks like that :

"cos(x2)+ln(x1)+exp(x2)+3*sqrt(x3)+1/ln(x2)+3.1415+exp(ln(x5))+arccos(sin(x4))+cos(x1)"

I want to replace cos by Math.cos, ln by Math.log, arccos by Math.acos
etc etc ...So my final aim would be :

"Math.cos(x2)+Math.log(x1)+Math.exp(x2)+3*Math.sqrt(x3)+1/Math.log(x2)+3.1415+Math.exp(Math.log(x5))+Math.acos(Math.sin(x4))+Math.cos(x1)"

I tried to successivily apply a replaceAll() method on my string, with
different replacement strings.
I had a problem with cos and arccos, as with sin and arcsin or tan and
arctan ... indeed, if i make something like :

myStr = myStr.replaceAll("cos\\(","Math.cos(");

Then I have a problem because it replaces arccos( by arcMath.cos( ...
And I don't want that ! :)
The same problem occurs if I replace first arccos then cos .... the
mistake is just different ;)

So I decided to do it another way, using a pattern and a matcher ...
But I don't succeed in finding the good pattern !
Indeed, I'm trying to find a pattern that matches "cos(" but doesn't
match "arccos".

Can anyone help me with this problem ? Have a link ? Thanks.

@++
Pimousse

PS : I've already read
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://java.sun.com/developer/JDCTechTips/2002/tt1008.html#1
http://java.sun.com/docs/books/tutorial/extra/regex/index.html
So pleased don't give me these links to read ;)

Try this one: http://www.regular-expressions.info/

To avoid matching the "cos" inside "arccos", you would use the
word-boundary construct, \b:

myStr = myStr.replaceAll("\\bcos\\(", "Math.cos(");

The functions whose names stay the same can be done all in one pass
using alternation and capturing groups:

myStr = myStr.replaceAll("\\b(cos|sin|exp|sqrt)\\(", "Math.$1(");

But you still have to make a separate pass for each of the other
functions. It's best to do this kind of thing in a single pass,
though; it's faster and (as you've seen) less error-prone. It may not
be worth the effort in your case, but here's a way you can do this job
in one pass:


Map table = new HashMap();
table.put("arccos", "acos");
table.put("ln", "log");
table.put("cos", "cos");
table.put("sin", "sin");
table.put("exp", "exp");
table.put("sqrt", "sqrt");

Pattern p = Pattern.compile(
"\\b(arccos|ln|cos|sin|exp|sqrt)(?=\\()");

Matcher m = p.matcher(myStr);
StringBuffer sb = new StringBuffer();

while(m.find())
{
m.appendReplacement(sb, "Math.");
sb.append(table.get(m.group(1)));
}
m.appendTail(sb);

myStr = sb.toString();


Using lookahead to check for the open parenthesis lets me match (and
replace) just the function name, which keeps the code much neater.
 
P

Pimousse

Thanks all of us !
I finally used the Cid's solution, with a replaceAll !
I didn't see at first glance the concept of boundaries.

@++
 
R

Roedy Green

So I decided to do it another way, using a pattern and a matcher ...
But I don't succeed in finding the good pattern !
Indeed, I'm trying to find a pattern that matches "cos(" but doesn't
match "arccos".

Perhaps you could tackle it without regexes. Parse the expression
into tokens, then look up each token in a HashMap to see if it has a
replacement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top