Simple regex replace

G

gwoodhouse

Hello,

Quick one:

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

Whats your guys suggestion?

Graeme
 
E

Eric Sosman

Hello,

Quick one:

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

This looks right, and works for me. Just guessing: Do you
keep the String that replaceAll() returns, or do you just throw
it away (imagining, perhaps, that replaceAll() modifies its own
immutable String object)?
 
L

Lew

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

This works for me:
<sscce comment="run as a JUnit test">
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RegexTest
{
@test
public void testRegex()
{
final String ts = "<![CDATA[This is a test.";
final String exp = "This is a test.";

final String tx = ts.replaceAll( "<!\\[CDATA\\[", "");
assertEquals( exp, tx );
}
}
</sscce>

So what exactly is your problem? What are you doing that you didn't
tell us?

You really, really, really, really, really, really, really should
provide an SSCCE. Obviously your problem is in the part you are not
revealing to us.
<http://sscce.org/>
 
G

gwoodhouse

Quick one:
I'm trying to remove this from a String: "<![CDATA["
But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

     This looks right, and works for me.  Just guessing: Do you
keep the String that replaceAll() returns, or do you just throw
it away (imagining, perhaps, that replaceAll() modifies its own
immutable String object)?

Thanks all,

Literally, i was doing a .replaceAll and then not assigning the return
to anything. I'm a bad man.

Thanks to Bent C/D
 
J

Jim Janney

Hello,

Quick one:

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

Whats your guys suggestion?

tagContents.replaceAll(Pattern.quote("<![CDATA["), "");
 
L

Lew

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

Whats your guys suggestion?

Jim said:
tagContents.replaceAll(Pattern.quote("<![CDATA["), "");

This has the exact same effect as the OP's code.

It's a useful idiom to know.
 
D

Daniel Pitts

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

Whats your guys suggestion?

Jim said:
tagContents.replaceAll(Pattern.quote("<![CDATA["), "");

This has the exact same effect as the OP's code.

It's a useful idiom to know.

You're statements are both true.

So are both of mine. :)
 
E

Eric Sosman

Although, not both of my statements were spelled correctly.
"You're"->"Your" ;-)

If youd kept quiet about it, Idve thought it was in homage
to the O.P.s missing apostrophes in "Whats your guys suggestion?"
Arent you sorry you self-respond'ed?
 
D

Daniel Pitts

If youd kept quiet about it, Idve thought it was in homage
to the O.P.s missing apostrophes in "Whats your guys suggestion?"
Arent you sorry you self-respond'ed?
N'pe N't at all.
 
A

Arne Vajhøj

Quick one:

I'm trying to remove this from a String: "<![CDATA["

But when using this line it ignores it (Would usually just single
escape it but java doesn't like that either):
tagContents.replaceAll("<!\\[CDATA\\[", "");

When using this one it throws an execption (With good cause since [ is
a regex special character):
tagContents.replaceAll("<![CDATA[", "");

Whats your guys suggestion?

Besides the already stated about using return value and literal,
then I will say that you should consider using one of Java's many
XML libraries instead of using regex to manipulate XML with.

Arne
 
R

RedGrittyBrick

Agreed. It's sad that XML, the touted "simple" replacement/successor
to SGML became so complicated that a library (and a large one!) is
needed, but it is.

Amen.

Actually several large libraries. Choosing between them is a problem in
itself, at least for newbies.
 
L

Lew

bugbear said:
Agreed. It's sad that XML, the touted "simple" replacement/successor
to SGML became so complicated that a library (and a large one!) is
needed, but it is.

You say that as if you found something wrong with it.
 
M

Mike Amling

bugbear said:
Agreed. It's sad that XML, the touted "simple" replacement/successor
to SGML became so complicated that a library (and a large one!) is
needed, but it is.

Yep. That's why I wrote my own implementations of an XML parser,
formatter and validator (DSD), years ago. I got tired of shipping two
giant jar files that served no other purpose. It was worth the effort
just to get the ability to use DSD validation rather than XML Schema.
Note: I'm not advocating this for everybody. But it sure works for me
(and my colleagues).
I expect this post may generate some controversy.

--Mike Amling
 
A

Arne Vajhøj

Agreed. It's sad that XML, the touted "simple" replacement/successor
to SGML became so complicated that a library (and a large one!) is
needed, but it is.

XML is not a replacement for SGML. XML is a form of SGML.

And I do not see the benefits of an existing library as
an indication of a problem. It is natural to reuse code.

I do agree that XML has become rather complex. There are
some "design by committee" signs in it. DTD is much simpler
than schemas. Sure schemas can provide more information, but
is it really needed.

Arne
 
A

Arne Vajhøj

Amen.

Actually several large libraries. Choosing between them is a problem in
itself, at least for newbies.

One has to evaluate whether the benefits outweigh the cost of
the extra complexity.

I think JAXB and StAX was worth it.

Arne
 
A

Arne Vajhøj

Yep. That's why I wrote my own implementations of an XML parser,
formatter and validator (DSD), years ago. I got tired of shipping two
giant jar files that served no other purpose. It was worth the effort
just to get the ability to use DSD validation rather than XML Schema.

Given that XML parser has required no extra jar files since
Java 1.4 released in 2002, then that argument has not much
validity today.

Arne
 
L

Lew

One has to evaluate whether the benefits outweigh the cost of
the extra complexity.

I think JAXB and StAX was worth it.

Even SAX, and occasionally one of the DOMs.

JAXB is especially painless.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top