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
 
Ad

Advertisements

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
 
Ad

Advertisements

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.
 

Top