Ali
Ali

Reputation: 267267

Regular expression to match strings enclosed in square brackets or double quotes

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")

Upvotes: 2

Views: 20794

Answers (3)

Chas. Owens
Chas. Owens

Reputation: 64939

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";

Upvotes: 2

Peter Boughton
Peter Boughton

Reputation: 112230

\[\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.

Upvotes: 13

jSheely
jSheely

Reputation:

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 ""

Upvotes: 0

Related Questions