Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Expression 1

Status
Not open for further replies.

Edge118

Programmer
Jun 15, 2004
104
0
0
US
Code:
line = line.replaceAll("?/)&+%%!", "********");

I'm having trouble representing ?/)&+%%! as a regex. As is, I am getting the error: Illegal escape characters for the ? and )

Thanks for your help!


"Pin me. Perl me."

Regards,

Chris
 
Put backslashes in front of them?

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
I already tried that and it gives me the same error.

"Pin me. Perl me."

Regards,

Chris
 
In perl and javascript, regexp is between foreslashes (/regex/). I don't know about java, but you can try that.

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
I don't think it's the same situation here.

"Pin me. Perl me."

Regards,

Chris
 
it's not, but you need doubled escape-sequences.

A special-meaning character like ')' needs to be escaped, when meant as itself.
But java will interpret '\)' the first backslash as escape-character itself, so you have to show java, that '\' itself is meant, so you end up '\\)'.

That's only the beginning.
Think of java-code, which generates intermediate output for javascript, then you have '\\\\)' and now, the javascript will be in a webpage, and show the usage of regexp, so to display 4 '\' you will need: ....?

:)

seeking a job as java-programmer in Berlin:
 
Thanks for your help...here's what I changed it to:

Code:
line = line.replaceAll("\\?/\\)&+%%!", "********");

Is that the equivalent to: "?/)&+%%!"

Cause it's not replacing it in my text file. Thanks.

"Pin me. Perl me."

Regards,

Chris
 
If you don't like to read the (not very lean) javadoc-pages, to find the solution, you make a small test-program:
Code:
final String oriLine = "?/)&+%!";
System.out.println (oriLine.replaceAll("\\?", "1"));
System.out.println (oriLine.replaceAll("\\)", "2"));
// ...
And so on.
This way you find very fast (took me 2 mins) which character needs to be escaped.

For later usage, it's good practice, to have a snipplet-template, which creates a testfile, named dynamically, containing a class, a main method and constructor, where you only need to insert your testcode, but which can be saved for later usage.
The dynamic name is important, to store different tests without loosing time, and to work on multiple testings at the same time.

I just called
Code:
jtemplate SuspRegex
and could start testing.
You will not believe how many time you save, if you spend it once to generate a template.

seeking a job as java-programmer in Berlin:
 
Thanks for the help. It turns out my problem was I needed "\\" before the "?", ")", AND the "+"

Thanks again!

"Pin me. Perl me."

Regards,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top