How do I parse this string with regex?
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 mat开发者_如何学运维ch");
}
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("\"", "");
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.)
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");
}
Try thi Regex :
^"(?<text>[^"]+?)"
I think you were missing second " (the one after GOOG")
EDIT :
Pattern pattern = Pattern.compile("^\"([^\"]+)\"");
精彩评论