• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String replaceAll()

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try \\s
[ February 28, 2005: Message edited by: Max Habibi ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it finally!
The final regex looks like this \\<\\?xml\\sversion\\s=\\s'1.0'\\?\\>

Thanks guys!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic