tags:

views:

149

answers:

3

I need 2 simple reg exps that will:

  1. Match if a string is contained within square brackets ([] e.g [word])
  2. Match if string is contained within double quotes ("" e.g "word")

Thanks in advance.

+8  A: 
\[\w+\]

"\w+"


Explanation:

The \[ and \] escape the special bracket characters to match their literals.

The \w means "any word character", usually considered same as alphanumeric or underscore.

The + means one or more of the preceding item.

The " are literal characters.


NOTE: If you want to ensure the whole string matches (not just part of it), prefix with ^ and suffix with $.


And next time, you should be able to answer this yourself, by reading regular-expressions.info

Update:

Ok, so based on your comment, what you appear to be wanting to know is if the first character is [ and the last ] or if the first and last are both " ?
If so, these will match those:

^\[.*\]$    (or ^\\[.*\\]$ in a Java String)

"^.*$"

However, unless you need to do some special checking with the centre characters, simply doing:

if ( MyString.startsWith("[") && MyString.endsWith("]") )

and

if ( MyString.startsWith("\"") && MyString.endsWith("\"") )

Which I suspect would be faster than a regex.

Peter Boughton
I had to change this to ^\\[\\w+\\]$ to make it work in Java without giving compile errors, will it work?
Click Upvote
You didn't change the regex, so it will still work - all you changed was the string representation of it, because backslashes require escaping in Java when defining strings.
Peter Boughton
Also: "will it work?" -- try it and find out!!!
Peter Boughton
Ok, it does work, but your regexp fails if there are multiple double quotes or squares in a word, e.g "this is a "long" word" fails. Is there any way to check only the start and end ?
Click Upvote
Also, it fails with special chars, i'd like to allow every thing as long as it was between squares or double quotes
Click Upvote
I'm not 100% sure what you're asking, but I'll edit my question to see if I can answer you anyway...
Peter Boughton
Also yours doesn't work if there are multiple words/phrases like "this is a long keyword"..
Click Upvote
Of course it doesn't - that wasn't part of the original question!!! See my edited answer, it might now do what you want?If not, please edit the question to provide *full* details of what you're after, and what you're trying to do, instead of being short, vague, and incomplete.
Peter Boughton
Thanks, I did go with the startsWith() and endsWith() in the end, heh.
Click Upvote
A: 

Are they two separate expressions?

[[A-Za-z]+]

\"[A-Za-z]+\"

If they are in a single expression:

[[\"]+[a-zA-Z]+[]\"]+

Remember that in .net you'll need to escape the double quotes " by ""

I can see that this will work only with alphabets, can you change it so it works with all characters, alphanumeric and special, as long as they were between square brackets or double quotes?
Click Upvote
+2  A: 

Important issues that may make this hard/impossible in a regex:

  1. Can [] be nested (e.g. [foo [bar]])? If so, then a traditional regex cannot help you. Perl's extended regexes can, but it is probably better to write a parser.

  2. Can [, ], or " appear escaped (e.g. "foo said \"bar\"") in the string? If so, see How can I match double-quoted strings with escaped double-quote characters?

  3. Is it possible for there to be more than one instance of these in the string you are matching? If so, you probably want to use the non-greedy quantifier modifier (i.e. ?) to get the smallest string that matches: /(".*?"|\[.*?\])/g

Based on comments, you seem to want to match things like "this is a "long" word"

#!/usr/bin/perl

use strict;
use warnings;

my $s = 'The non-string "this is a crazy "string"" is bad (has own delimiter)';

print $s =~ /^.*?(".*").*?$/, "\n";
Chas. Owens