This should be easy...

T

Tom McCallum

Hi,

I am trying to replace all words starting with a question mark - how do
you do this? Have tried everything I can think of to get Java to not use
? as just being a character but it keeps on failing.

Example:
String to be replaced: My name is ?myname
Replace with string: Tom
Problem: how to write a regular expression in Java to replace '?myname'
with 'Tom'.

Many thanks for your help in advance

Tom
 
K

klynn47

One idea without regular expressions would be to use the charAt method
and the UNICODE representation of ?
 
T

Tom McCallum

Tried it, Java then converts it to a question mark and throws a runtime
wabbly :) Problem is Java does not seem to support the escape sequence \?
so putting ? or \? is a no-no.

Cheers for your reply

Tom
 
P

Patricia Shanahan

Tom said:
Hi,

I am trying to replace all words starting with a question mark - how do
you do this? Have tried everything I can think of to get Java to not
use ? as just being a character but it keeps on failing.

Example:
String to be replaced: My name is ?myname
Replace with string: Tom
Problem: how to write a regular expression in Java to replace '?myname'
with 'Tom'.

Many thanks for your help in advance

Tom

Use "\\?myname".

The double "\" puts a single "\" in the String literal,
which turns the "?" into a normal character.

Patricia
 
V

Virgil Green

Tom said:
Tried it, Java then converts it to a question mark and throws a
runtime wabbly :) Problem is Java does not seem to support the
escape sequence \? so putting ? or \? is a no-no.

You need to double the backslash so it is escaped to a single backslash in
java that can be passed to the regex parser.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top