Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

String problem: Not being able to escape a parenthesis for a possible regex

821613Feb 28 2012 — edited Feb 28 2012
So, I have barely any clue how to operate regular expressions except for a little bit of knowledge of it in PHP.

What I'm trying to do is to separate the first part of a string from a qualifier, so I need a regular expression since I would ideally like to be able . The problem I'm having is that my attempt at the separation isn't working. I've tried doing what I think the Pattern or Matcher class does, I've tried using String.split, and I've tried using the regular String.replace method. What I'm trying to do is illustrated (I hope) in the following SSCCE:
public class SSCCE
{
    public static void main(final String args[])
    {
        final String testString = "I want to keep this sentence. (I wish to "+
            "discard this sentence and the space in between the sentences.)";
        final String testExtract = testString.replace(" \(.+\)","");
        System.out.println(testExtract);
    }
}
From my understanding, parentheses are supposed to be a wild card of sorts, so they would need to be escaped in order to be taken as literals, which means that line 7 is supposed to match any of the characters inside the parentheses when the opening one is preceeded by a space and then delete them. The problem I'm having is that escaping the parentheses produces an error, and not escaping them produces the original string. So I'm not sure how to tell Java that I'm actually using literal parentheses.

I already tried configuring my error preferences to ignore everything it could, but that didn't work.

I don't know if it matters, but I'm using Java 1.6 and Eclipse for PHP 1.3.2.20110218-0812.
This post has been answered by sabre150 on Feb 28 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2012
Added on Feb 28 2012
7 comments
1,183 views