Google Groups Home
Help | Sign in
Matching parentheses with Regular Expressions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
James  
View profile
 More options Jul 3, 9:12 pm
Newsgroups: comp.lang.java.programmer
From: James <jalantho...@verizon.net>
Date: Thu, 3 Jul 2008 18:12:55 -0700 (PDT)
Local: Thurs, Jul 3 2008 9:12 pm
Subject: Matching parentheses with Regular Expressions
   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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James  
View profile
 More options Jul 3, 9:23 pm
Newsgroups: comp.lang.java.programmer
From: James <jalantho...@verizon.net>
Date: Thu, 3 Jul 2008 18:23:19 -0700 (PDT)
Local: Thurs, Jul 3 2008 9:23 pm
Subject: Re: Matching parentheses with Regular Expressions
OK, I finally found the words about using double slashes in front of
parentheses.  So, now, why won`t the following regular expression
pattern compile?

private static final Pattern java_proc = Pattern.compile("\\\\.+\
\Process\\(java\\)\\");

The error says:

java.lang.ExceptionInInitializerError
Caused by: java.util.regex.PatternSyntaxException: Unknown character
property name {r} near index 6
\\.+\Process\(java\)\
      ^

This does not make sense to me.

    I`m trying to match text of the form (example):

\\GOLLY\Process(java)\% Processor Time

            Thanks, Alan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Cranmer  
View profile
 More options Jul 3, 9:31 pm
Newsgroups: comp.lang.java.programmer
From: Joshua Cranmer <Pidgeo...@verizon.invalid>
Date: Fri, 04 Jul 2008 01:31:52 GMT
Local: Thurs, Jul 3 2008 9:31 pm
Subject: Re: Matching parentheses with Regular Expressions

James wrote:
> OK, I finally found the words about using double slashes in front of
> parentheses.  So, now, why won`t the following regular expression
> pattern compile?

> private static final Pattern java_proc = Pattern.compile("\\\\.+\
> \Process\\(java\\)\\");

> The error says:

> java.lang.ExceptionInInitializerError
> Caused by: java.util.regex.PatternSyntaxException: Unknown character
> property name {r} near index 6
> \\.+\Process\(java\)\
>       ^

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James  
View profile
 More options Jul 3, 9:44 pm
Newsgroups: comp.lang.java.programmer
From: James <jalantho...@verizon.net>
Date: Thu, 3 Jul 2008 18:44:50 -0700 (PDT)
Local: Thurs, Jul 3 2008 9:44 pm
Subject: Re: Matching parentheses with Regular Expressions
   Thank you.

   I have one last remaining problem.  The full data I`m working with,
in CSV format, looks like this:

"(PDH-CSV 4.0) (Eastern Daylight Time)(240)","\\GOLLY\Memory\%
Committed Bytes In Use","\\GOLLY\Process(java)\% Processor Time"

I want to match on

\\GOLLY\Process(java)\

so I can replace it.

    The regular expression

\\\\{2}.+\\\\Process\\(java\\).

matches, but it matches too much of it:

\\GOLLY\Memory\% Committed Bytes In Use","\\GOLLY\Process(java)\

    How can I get it to only match the part I want?

                    Thanks again, Alan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Cranmer  
View profile
 More options Jul 3, 9:52 pm
Newsgroups: comp.lang.java.programmer
From: Joshua Cranmer <Pidgeo...@verizon.invalid>
Date: Fri, 04 Jul 2008 01:52:10 GMT
Local: Thurs, Jul 3 2008 9:52 pm
Subject: Re: Matching parentheses with Regular Expressions
James wrote:
>     The regular expression

> \\\\{2}.+\\\\Process\\(java\\).

 >
 > matches, but it matches too much of it:

In that case, you probably want this regex:
\\\\{2}[^\\\\]+\\\\Process\\(java\\)
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arved Sandstrom  
View profile
 More options Jul 3, 10:22 pm
Newsgroups: comp.lang.java.programmer
From: "Arved Sandstrom" <asandst...@accesswave.ca>
Date: Fri, 04 Jul 2008 02:22:18 GMT
Local: Thurs, Jul 3 2008 10:22 pm
Subject: Re: Matching parentheses with Regular Expressions
"James" <jalantho...@verizon.net> wrote in message

news:34ef4eab-fc10-4809-9fb8-f5ab8edfa7b5@27g2000hsf.googlegroups.com...

Double backslash your pattern: \\(java)\\

AHS


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roedy Green  
View profile
 More options Jul 4, 12:23 am
Newsgroups: comp.lang.java.programmer
From: Roedy Green <see_webs...@mindprod.com.invalid>
Date: Fri, 04 Jul 2008 04:23:49 GMT
Local: Fri, Jul 4 2008 12:23 am
Subject: Re: Matching parentheses with Regular Expressions
On Thu, 3 Jul 2008 18:12:55 -0700 (PDT), James
<jalantho...@verizon.net> wrote, quoted or indirectly quoted someone
who said :

>    private static final Pattern java_proc = Pattern.compile("\(java
>\)");

It gets complicated because you have both Java and regex escape
quoting.

See http://mindprod.com/jgloss/regex.html#QUOTING

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
shakah  
View profile
 More options Jul 4, 8:04 am
Newsgroups: comp.lang.java.programmer
From: shakah <shakahsha...@gmail.com>
Date: Fri, 4 Jul 2008 05:04:16 -0700 (PDT)
Local: Fri, Jul 4 2008 8:04 am
Subject: Re: Matching parentheses with Regular Expressions
On Jul 3, 9:52 pm, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:

> James wrote:
> >     The regular expression

> > \\\\{2}.+\\\\Process\\(java\\).

>  > matches, but it matches too much of it:

> In that case, you probably want this regex:
> \\\\{2}[^\\\\]+\\\\Process\\(java\\)
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

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[)]

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Space  
View profile
 More options Jul 4, 2:36 pm
Newsgroups: comp.lang.java.programmer
From: Mark Space <marksp...@sbc.global.net>
Date: Fri, 04 Jul 2008 11:36:12 -0700
Local: Fri, Jul 4 2008 2:36 pm
Subject: Re: Matching parentheses with Regular Expressions

You also might get rid of some of those backslashes by substituting
another character, then using replace() on the string before compiling it.

       final static String PATTERN = "``{2}.+``Process`(java`)";

       String myRegex = PATTERN.replace("`", "\\" );
       System.out.println( myRegex );

Result:

\\{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.

Just thinking out loud....


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roedy Green  
View profile
 More options Jul 4, 2:50 pm
Newsgroups: comp.lang.java.programmer
From: Roedy Green <see_webs...@mindprod.com.invalid>
Date: Fri, 04 Jul 2008 18:50:01 GMT
Local: Fri, Jul 4 2008 2:50 pm
Subject: Re: Matching parentheses with Regular Expressions
On Fri, 04 Jul 2008 11:36:12 -0700, Mark Space
<marksp...@sbc.global.net> 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.

Other ideas:

1. Use Quoter to insert \ quoting, both for regex and Java strings.
see http://mindprod.com/applet/quoter.html

2. implement one or more of my regex student projects
http://mindprod.com/project/regexutility.html
http://mindprod.com/project/regexcomposer.html
http://mindprod.com/project/regexdebugger.html
http://mindprod.com/project/regexproofreader.html

3. use \Q ... \E
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Space  
View profile
 More options Jul 4, 3:05 pm
Newsgroups: comp.lang.java.programmer
From: Mark Space <marksp...@sbc.global.net>
Date: Fri, 04 Jul 2008 12:05:48 -0700
Local: Fri, Jul 4 2008 3:05 pm
Subject: Re: Matching parentheses with Regular Expressions

Roedy Green wrote:
> 3. use \Q ... \E

OK, that's cool.  It only works with regex, but it's darn handy for
them.  Thanks!

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James  
View profile
 More options Jul 5, 3:48 pm
Newsgroups: comp.lang.java.programmer
From: James <jalantho...@verizon.net>
Date: Sat, 5 Jul 2008 12:48:44 -0700 (PDT)
Local: Sat, Jul 5 2008 3:48 pm
Subject: Re: Matching parentheses with Regular Expressions
shakah,

   The statement

            Pattern JAVA_PROC = Pattern.compile("[\\]{2}[^\\]+[\
\]Process[(]java[)]");

compiles but raises an exception there:

run:
Exception in thread "main" java.util.regex.PatternSyntaxException:
Unclosed character class near index 30
[\]{2}[^\]+[\]Process[(]java[)]
                              ^

All:  Thank you for your suggestions.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.