3

I have a string like:

"GOOG",625.00,"-1.95 - -0.31%"

I'm using this pattern, and it isn't matching. I'm trying to get GOOG. What am I doing wrong?

Pattern pattern = Pattern.compile("^\"([^\"]+)");
Matcher matcher = pattern.matcher(line);

if (matcher.matches()) {
    Log.i(TAG, matcher.group(0));
} else {
    Log.i(TAG, "no match");
}
2
  • The pattern looks fine to me -- can you verify by writing out the first few characters that "line" doesn't begin with a newline or something crazy like that? Commented Nov 10, 2010 at 3:12
  • Using regex to parse something is dead inefficient. Rather use it to match something and use a fullworthy parser to parse something. For CSV there exist CSV parsers (as HTML parsers exist for HTML...). Commented Nov 10, 2010 at 4:35

4 Answers 4

3

The problem is you're not running matcher.find() so the expression is never really evaluated. What you have will work fine if you just change it to:

if (matcher.find()) {

Though this seems like it'd be easier if you just used the String.split method (or better yet, use a library for parsing CSV files):

String temp = "\"GOOG\",625.00,\"-1.95 - -0.31%\"";
String[] parts = temp.split(",");
String symbol = temp[0].replaceAll("\"", "");
Sign up to request clarification or add additional context in comments.

2 Comments

"(or better yet, [use] a library for parsing CSV files)"
Assuming GOOG will never have a comma in it but I think that's unlikely since it would annoy a lot of stock traders :-)
1

Java's matches() method expects the regex to match the whole string, as if it were anchored at both ends with ^ and $ (or \A and \z). Whenever you use matches() with a regex that only matches part of the string, you need to "pad" the regex with .*, like so:

Pattern pattern = Pattern.compile("\"([^\"]+).*");
Matcher matcher = pattern.matcher(line);

if (matcher.matches()) {
    Log.i(TAG, matcher.group(1));  // not group(0)!
} else {
    Log.i(TAG, "no match");
}

The ^ at the beginning of the regex wasn't doing any harm, I just removed it to show that it wasn't necessary. Notice that I also changed your group(0) to group(1)--that was another error in your code. group(0) is the whole match, while group(1) refers only to the part that was matched in the first set of capturing parentheses.

You also have the option of using find(), like so:

Pattern pattern = Pattern.compile("\"([^\"]+)");
Matcher matcher = pattern.matcher(line);

if (matcher.find()) {
    Log.i(TAG, matcher.group(1));
} else {
    Log.i(TAG, "no match");
}

This matches the first instance of a quotation mark followed by one or more characters other than quotation marks (which are captured in group #1). That will match anywhere; if you want it to match only at the very beginning of the string, you have to use the ^ anchor as you had it in your original regex: "^\"([^\"]+)"

(There's also a lookingAt() method, which automatically anchors the match to the beginning of the string but not the end, but nobody ever uses it.)

Comments

0

you first need to call matcher.find()

String temp = "\"GOOG\",625.00,\"-1.95 - -0.31%\"";
Pattern pattern = Pattern.compile("\"[a-zA-Z]*\"");
Matcher matcher = pattern.matcher(temp);

if(matcher.find()){
    Log.i(TAG, matcher.group(0).split("\"")[1]);  
} else {  
    Log.i(TAG, "no match");  
}

Comments

0

Try thi Regex :

^"(?<text>[^"]+?)"

I think you were missing second " (the one after GOOG")

EDIT :

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

1 Comment

(?<XYZ>...) is how you specify a named group, but Java regexes don't support those yet (they're coming in Java 7). And leaving off the second quote is not an error in this case; the [^\"]+ will stop matching when it runs out of non-quotes. (But I would add the closing quote anyway, to avoid confusion.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.