tags:

views:

62

answers:

3

If I have a string such as

lotsofcrap"somethingimportant"moreotherstuff

is it possible to get Regex to match just whatever is between the " ", excluding the quotation marks? So the way to detect it would be something like ".*", but that will return "somethingimportant" rather than just pure somethingimportant

+1  A: 

If your regex engine supports zero-width assertions (look-behinds and look-aheads),

(?<=")[^"]*(?=")

will match a sequence of non-quote characters, where there occurs a quote before and a quote after.

However, this is silly. You should simply

"([^"]*)"

match everything, including the quotes, and then pull group 1 (the set of parentheses) out of the match.

ephemient
+3  A: 
"(.*)"

You can use parenthese to create a capturing group. How you access it depends on the language/library you're using--typically the capture groups are available as $1 or \1 in Perl-like languages. For example, in Perl:

'hello "world" !!!' =~ /"(.*)"/;
print "$1\n";
John Kugelman
+1 Beat me by 27 seconds :)
Si
Actually, it doesn't solve the problem completely, since the group won't contain the parentheses.
Si
Thank you, this is what I was looking for.
mrnye
+1  A: 

Try "(.*?)"

The ? means that the .* will expand as needed (until it matches the next )" in this case).

Java Code:

static String regex = "\"(.*?)\"";
static Pattern p = Pattern.compile(regex);

public static List<String> getMatches(String inputText) {
 Matcher m = p.matcher(inputText);
 List<String> list = new ArrayList<String>();
 while(m.find()){
  list.add(m.group(1));
 }
 return list;
}
hashable