This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replaceAll()

 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the following code to replace String
str = str.replaceAll("<?xml version = '1.0'?>","");
The str i get after running this has the <?xml version = '1.0'?> not replaced.

But if give some simple string like
str = str.replaceAll("xml",""); it works. Here the xml is replaced by blank.
Why is this happening? How do i get it working, is there any work around?

Thank you!
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first parameter of the replaceAll() method is a regex -- not just a straight string. You need to give it a valid regex that represents the pattern you are replacing.

Henry
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i find the regex for this particular string.
I found the Class Pattern in Sun's site that lists the regex

But when I replace space with \s I am getting a compilation error.
It would be really helpful if you can you show me an example how to convert a string to regex?
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try \\s
[ February 28, 2005: Message edited by: Max Habibi ]
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vivek ja:
How do i find the regex for this particular string.
I found the Class Pattern in Sun's site that lists the regex

But when I replace space with \s I am getting a compilation error.
It would be really helpful if you can you show me an example how to convert a string to regex?


This is one of the reasons that I don't like the Java regex library. You have to deal with *both* the regex library and Java. "\" has special meaning in a Java string to the compiler, so to get a "\" for the regex string, you need another "\".

Henry
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it finally!
The final regex looks like this \\<\\?xml\\sversion\\s=\\s'1.0'\\?\\>

Thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic