name_masked
name_masked

Reputation: 9794

Regular Expression - Java

For the string value "ABCD_12" (including quotes), I would like to extract only the content and exclude out the double quotes i.e. ABCD_12 . My code is:

private static void checkRegex()
{
    final Pattern stringPattern = Pattern.compile("\"([a-zA-Z_0-9])+\"");
    Matcher findMatches = stringPattern.matcher("\"ABC_12\"");
    if (findMatches.matches())
        System.out.println("Match found" + findMatches.group(0));
}

Now I have tried doing findMatches.group(1);, but that only returns the last character in the string (I did not understand why !).

How can I extract only the content leaving out the double quotes?

Upvotes: 2

Views: 182

Answers (4)

Vijay Agrawal
Vijay Agrawal

Reputation: 3821

If your pattern is strictly any text in between double quotes, then you may be better off using substring:

String str = "\"ABC_12\"";
System.out.println(str.substring(1, str.lastIndexOf('\"')));

Assuming it is a bit more complex (double quotes in between a larger string), you can use the split() function in the Pattern class and use \" as your regex - this will split the string around the \" so you can easily extract the content you want

Pattern p = Pattern.compile("\"");
    // Split input with the pattern
    String[] result = 
             p.split(str);
    for (int i=0; i<result.length; i++)
        System.out.println(result[i]);
    }

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#split%28java.lang.CharSequence%29

Upvotes: 1

Laf
Laf

Reputation: 8185

You should change your pattern to this:

final Pattern stringPattern = Pattern.compile("\"([a-zA-Z_0-9]+)\"");

Note that the + sign was moved inside the group, since you want the character repetition to be part of the group. In the code you posted, what you were actually searching for was a repetition of the group, which consisted in a single occurence of a single characters in [a-zA-Z_0-9].

Upvotes: 1

cjm
cjm

Reputation: 3753

A nice simple (read: non-regex) way to do this is:

String myString = "\"ABC_12\"";
String myFilteredString = myString.replaceAll("\"", "");
System.out.println(myFilteredString);

gets you

ABC_12

Upvotes: 1

anubhava
anubhava

Reputation: 784958

Try this regex:

Pattern.compile("\"([a-zA-Z_0-9]+)\"");

OR

Pattern.compile("\"([^\"]+)\"");

Problem in your code is a misplaced + outside right parenthesis. Which is causing capturing group to capture only 1 character (since + is outside) and that's why you get only last character eventually.

Upvotes: 6

Related Questions