How to Use Double Brackets in a Regular Expression

Regular expression double brackets [[text]]

Here it is, for selecting anything in between sets of [ and ] brackets:

((?!\])(?!\[).)+

Demo 1

eg.

var str = "[[[ some text]]]"
var res = str.match(/((?!\])(?!\[).)+/g);
console.log(res)

I think your edits made the question very different. Here is one that has strictly two starting and closing brackets.

^\[\[(((?!\[)(?!\]).)+)\]\]$

Demo 2

You could use group 1 for selecting text only.

How to use double brackets in a regular expression?

Posix character classes use a [:alpha:] notation, which are used inside a regular expression like:

/[[:alpha:][:digit:]]/

You'll need to scroll down a ways to get to the Posix information in the link above. From the docs:

POSIX bracket expressions are also similar to character classes. They provide a portable alternative to the above, with the added benefit that they encompass non-ASCII characters. For instance, /\d/ matches only the ASCII decimal digits (0-9); whereas /[[:digit:]]/ matches any character in the Unicode Nd category.

/[[:alnum:]]/ - Alphabetic and numeric character
/[[:alpha:]]/ - Alphabetic character
/[[:blank:]]/ - Space or tab
/[[:cntrl:]]/ - Control character
/[[:digit:]]/ - Digit
/[[:graph:]]/ - Non-blank character (excludes spaces, control characters, and similar)
/[[:lower:]]/ - Lowercase alphabetical character
/[[:print:]]/ - Like [:graph:], but includes the space character
/[[:punct:]]/ - Punctuation character
/[[:space:]]/ - Whitespace character ([:blank:], newline,
carriage return, etc.)
/[[:upper:]]/ - Uppercase alphabetical
/[[:xdigit:]]/ - Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)

Ruby also supports the following non-POSIX character classes:

/[[:word:]]/ - A character in one of the following Unicode general categories Letter, Mark, Number, Connector_Punctuation
/[[:ascii:]]/ - A character in the ASCII character set
# U+06F2 is "EXTENDED ARABIC-INDIC DIGIT TWO"

/[[:digit:]]/.match("\u06F2") #=> #<MatchData "\u{06F2}">
/[[:upper:]][[:lower:]]/.match("Hello") #=> #<MatchData "He">
/[[:xdigit:]][[:xdigit:]]/.match("A6") #=> #<MatchData "A6">

Problem of Regular expression double brackets

If the nesting depth of inner brackets is limited to 1:

\[(?:\[.*?]|.)*?]

See live demo.

This works by optionally consuming the entire pattern [...] within the outer brackets, or just . if no such inner group is found.

The unnecessary grouping brackets and unnecessary escaping of ] have been removed.


Or if you need to create group 1:

(\[(?:\[.*?]|.)*?])

See live demo.

Using regex to detect ONLY double braces

You can simply exclude the braces from the "contained string" with a character class:

strings = re.findall(r'\{\{[^{}]*\}\}', string)

Regular expression for text between double square brackets, but NOT between hyphens

This pattern is working for me:

(?!\[{2}\-[^(\-|\s)]*\-\]{2})\[{2}([^(?:\]{2})]*)\]{2}

Matching the pattern you don't want ([[-foobar-]]) is pretty easy, so this solution uses a negative look-ahead to ensure the following doesn't match that pattern, then grabs everything between double brackets as a capture group.

Test it out here: https://regex101.com/r/pxbwKo/2

Having some trouble with regex and double brackets

If the input follows exactly your pattern, then you can use this to make your regex non-greedy

p = re.compile(ur'\[\[.*?\]\]')
test_str = u"[[1,2,3],[3,5,3],[9,8,9]] aoeu [5,6,9] aoeu [[4,5,5]]"
print(re.findall(p, test_str))

To handle cases like [[1,2,3],[3,5,3],3], [1,2,3,[3,5,3],3] etc. use this regex

(\[[^\[\]]*\[.*?\][^\]\[]*\])

REGEX DEMO

IDEONE DEMO

What would be the Regex to match a word wrapped in double brackets

I think that is what you need.

string input = "I love to [[verb]] while I [[verb]].";
string pattern = @"(\[\[.+?\]\])";

string[] matches = Regex.Split( input, pattern );

foreach (string match in matches)
{
Console.WriteLine(match);
}

RegEx - Value inside double square brackets

Given:

var s = '[[32]] [Test] Lorem Ipsum [[16]] Lorem Ipsum [[2]] Test [BUG]';

You can use a look–ahead that matches one or more digits followed by ']]' but doesn't include the ']]' in the match:

s.match(/\d+(?=\]\])/g)  // ["32", "16", "2"]

Or, if lookahead isn't available, you can use:

s.match(/\[\[(\d+)\]\]/g).map(function(v){return v.replace(/\[+|\]+/g,'');})

though if map is available then probably look–ahead is too. If the input is multi–line, you may need the m flag also.



Related Topics



Leave a reply



Submit