This is what the regex is seeing. Don't forget that `\' is also a
metacharacter in regexes. So to match a '\' in regex requires you to use
'\\\\', which causes the regex to see '\\', which is what it uses to
match as a '\'. So the regex you're probably trying to compile:
"\\\\{2}.+\\\\Process\\(java\\)\\\\" (The {2} is so that you don't have
to type in 8 slashes)

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

"James" <> wrote in message
news:...
> I`m trying to use regex to match/replace a word in parentheses.
> The regular expression
>
> private static final Pattern java_proc =
> Pattern.compile("(java)");
>
> does not work, because parentheses are treated as groupings.
>
> Using "\" to designate the parentheses as literal characters does
> not work --- not sure why:
>
> private static final Pattern java_proc = Pattern.compile("\(java
> \)");
>
> I searched for and read a related post here, but it did not
> help. I seem to be having a different problem than they. Or I just
> don`t understand the post.
>
> What am I doing wrong? Thanks, Alan

FWIW, you could avoid a little of the backslash escape mess
by using single-char character classes, e.g.:
Pattern.compile("[\\]{2}[^\\]+[\\]Process[(]java[)]") ;
// ...outside of a Java string that'd be [\]{2}[^\]+
[\]Process[(]java[)]

It just makes things more readable. Using `, or %, or # in a string,
then replace that character with \'s before compiling it as a regex can
save your eyes.

Incidentally, I wonder if Sun could be convinced to add this themselves.
Maybe add a new operator/keyword altogether. Like # introduces new
keywords or operators. It's followed by the keyword or operator. This
just allows Sun to make new keywords or operators, with out breaking any
existing code. So #s might give us new string constatns. Let's say '
then means like a Unix shell string, where escaping is ignored.

String regex = #s'\\{2}.+\\Process\(java\)';

Would give that literal string, without the need to escape the
backslashes. Easier for regex at least. Other types of flags besides '
could be introduced too. `,$,@,%,= might do the same thing, just use a
different character as a string terminator, in case you want a ' to be
part of the string. """ might introduce a "here-is" operator. Etc.

On Fri, 04 Jul 2008 11:36:12 -0700, Mark Space
<> wrote, quoted or indirectly quoted someone
who said :
>You also might get rid of some of those backslashes by substituting
>another character, then using replace() on the string before compiling it.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!