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





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


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


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

Which I suspect would be faster than a regex.

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"


use strict;
use warnings;

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

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

Are they two separate expressions?



If they are in a single expression:


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

Category:java Time:2009-04-05 Views:1
Tags: regex java

Related post

  • Regular expression to remove suffixes enclosed in square brackets from comma-delimited list 2010-10-16

    I need to format the followin string name[Name], emloyeeno[Employee Number], joindate[Date Of Join:dd MMM yyyy], email[EMail] as name, employeeno, joindate, email I'm trying to use a regular expression to resolve this, I best I could come up with is

  • Regular Expression for matching string like: "++" 2010-02-25

    Regular Expression for matching string like: "<any_string>+<any_string>+<any_string>"? --------------Solutions------------- It sounds as simple as: .*\+.*\+.* the .* matches any_string until the \+ matches a "+" symbol. This matches

  • Regular expression to match string not containing a word? 2009-01-02

    I know it is possible to match for the word and using tools options reverse the match. (eg. by grep -v) However I want to know if it is possible using regular expressions to match lines which does not contain a specific word, say hede? Input: Hoho Hi

  • Regular Expression to match string starting with "stop" 2009-08-06

    How do I create a regular expression to match a word at the beginning of a string. We are looking to match stop at the beginning of a string and anything can follow it. For example the expression should match: stop stop random stopping Thanks. ------

  • Regular Expression to Match String Exactly? 2010-08-13

    I'll preface this question by mentioning that while I'm far from a regular expressions guru, they are not completely foreign to me. Building a regular expression to search for a pattern inside a particular string generally isn't a problem for me, but

  • Android regular expression - return matched string 2012-02-20

    In my Android project I have a regular expression and a string, in which I should have the matched expression. The problem is, I've only found a matches() method, which returns boolean. Is there something, which returns only the matched string (for i

  • Javascript Regular Expression to match string unless preceeded by backslash 2009-09-04

    How do I match U1234, but not \U1234 in Javascript? I can't figure out how to not match the single backslash. The closest I can get is: \[\\]{0}U[0-9]{4}\b But that doesn't work. Any suggestions? --------------Solutions------------- JavaScript defini

  • How do I use a regular expression to match strings that don't start with an empty space? 2010-03-02

    I want a regular expression that checks that a string doesn't start with an empty space. I want to do something like this: Is the following ValidationExpression right for it? string ValidationExpression = @"/^[^ ]/"; if (!String.IsNullOrEmpty(GroupNa

  • Regular expression that matches string equals to one in a group 2012-01-12

    E.g. I want to match string with the same word at the end as at the begin, so that following strings match: aaa dsfj gjroo gnfsdj riier aaa sdf foiqjf skdfjqei adf sdf sdjfei sdf rew123 jefqeoi03945 jq984rjfa;p94 ajefoj384 rew123 --------------Soluti

  • Regular expression to match string of 0's and 1's without '011' substring 2010-04-17

    I'm working on a problem (from Introduction to Automata Theory, Languages and Computer by Hopcroft, Motwani and Ullman) to write a regular expression that defines a language consisting of all strings of 0s and 1s not containing the substring 011. Is

  • Regular expression to match strings that contain but not other HTML tags? 2010-11-15

    I want to find and match strings that contain no HTML tags but <br/> and all other normal characters ([^<>]+). So basically, this match dismisses any string that contains '<' or '>' but not '<br/>'. This is what I can come up

  • Regular Expression to match underscores not surrounded by brackets? 2009-03-24

    Can anyone suggest a regex to match the underscore in the following examples: test_test test[_test test_] But NOT match this: test[_]test This is using the .Net Regular Expression library. I'm using this RegEx tester to check:

  • Regular Expression to get strings enclosed in braces 2010-08-11

    I want to get an array of the following types of strings: data {string1} {string2} {string3} data I want to get an array whose values are string1/string2/strin3. How can I do this? --------------Solutions------------- one-liner w/o capturing parenthe

  • regular expression to match string 2011-04-30

    i want a pattern to match below strings : count="2" count = "2" count = "2" count='2' count = '2' count = '2' count=2 count = 2 count = 2 Thanks in advance --------------Solutions------------- It isn't too clear what should the pattern not match, but

  • Regular expression to match specific words + content inside brackets 2011-08-16

    I'm creating a post system, where I will tag external images, Flash, SL etc. with [image:http://anything/anyimage.jpg] or [silverlight:http://anothersite/ilovesilverlight.xap] etc. I'll be rendering the object tags for HTML upon finding the links. I'

  • Regular expression not matching specific string 2009-12-10

    My use case is as follows: I would like to find all occurrences of something similar to this /name.action, but where the last part is not .action eg: name.actoin - should match name.action - should not match nameaction - should not match I have this:

  • Regular expression for hexadecimal string in python not working 2012-04-22

    I have a regular expression to match strings like: --D2CBA65440D --77094A27E09 --77094A27E --770 --77094A27E09-- basically, it matches a hexadecimal string surrounded by one or more line breaks or white space, and has the prefix -- and may or may not

  • PHP Regular expression to match unescaped double quotes 2011-08-02

    I'm trying to figure out a regular expression that will detect if there is an unescaped double quote within two double quotes. I have this, but it's not working: if ( preg_match( '/^(\".*((?<!\\)\").*\")$/', $str ) ) { die("hey"); } --------------

  • How can I convert a string into a regular expression that matches itself in Perl? 2009-10-21

    How can I convert a string to a regular expression that matches itself in Perl? I have a set of strings like these: Enter your selection: Enter Code (Navigate, Abandon, Copy, Exit, ?): and I want to convert them to regular expressions sop I can match

Copyright (C), All Rights Reserved.

processed in 0.748 (s). 13 q(s)