Lokesh Bachani
Lokesh Bachani

Reputation: 237

Java - RegEx to replace text between dollar signs

I am using JAVA and want to replace every instance of text between dollar signs. For example:

1st equation $\frac{1}{\mu -1}\frac{2\pi }{\lambda }x$ 
2nd equation $90^{^{0}}$
3rd equation $\frac{\mu t}{2}$
4th equation $2\mu tcosr=\frac{\left ( 2n+1 \right ) \lambda}{2}$

to be replaced by this

1st equation <img src="http://latex.codecogs.com/gif.latex?$\frac{1}{\mu -1}\frac{2\pi }{\lambda }x$ " border="0"/>
2nd equation <img src="http://latex.codecogs.com/gif.latex?$90^{^{0}}$" border="0"/>
3rd equation <img src="http://latex.codecogs.com/gif.latex?$\frac{\mu t}{2}$" border="0"/>
4th equation <img src="http://latex.codecogs.com/gif.latex?$2\mu tcosr=\frac{\left ( 2n+1 \right ) \lambda}{2}$" border="0"/>

i searched on stackoverflow.com and found something similar for C#.NET RegEx to replace text between dollar signs

Upvotes: 1

Views: 1155

Answers (2)

John B
John B

Reputation: 32949

I believe it would be something like this...

myString.replaceAll("\\$[^$]*\\$", 
     "<img src=\"http://latex.codecogs.com/gif.latex?$0 \" border=\"0\"/>"

The $0 in the replacement string should match the capturing group in the search regex per...

String.replaceAll

Matcher.replaceAll

Upvotes: 4

m0skit0
m0skit0

Reputation: 25873

The regex used in C# is the same for Java, except that you need to double escape $.

"\\$([^\\$]*)\\$"

Upvotes: 1

Related Questions