Regex to match words inside two or three curly braces
Here is something that might work for you:
(?<!\{)\{\{(?:([^{}]+)|\{([^{}]+)})}}(?!})
See the online demo
- Words that are between exactly 2 curly brackets will be in group 1.
- Words that are between exactly 3 curly brackets will be in group 2.
(?<!\{)
- Negative lookbehind for opening curly bracket.\{\{
- Two literal curly (opening) brackets.(?:
- Open non-capture group.([^{}]+)
- A 1st capture group holding 1+ non-opening/closing brackets.|
- Or:\{([^{}]+)}
- A 2nd capture group with leading and trailing brackets.)
- Close non-capture group.
}}
- Two literal curly (closing) brackets.(?!})
- Negative lookahead for closing curly bracket.
Regex for matching all words between a set of curly braces
You're close, but using the wrong method:
sentence = "hello {name} is {thing}"
sentence.scan(/\{(.*?)\}/)
# => [["name"], ["thing"]]
Regex to get string between curly braces
If your string will always be of that format, a regex is overkill:
>>> var g='{getThis}';
>>> g.substring(1,g.length-1)
"getThis"
substring(1
means to start one character in (just past the first {
) and ,g.length-1)
means to take characters until (but not including) the character at the string length minus one. This works because the position is zero-based, i.e. g.length-1
is the last position.
For readers other than the original poster: If it has to be a regex, use /{([^}]*)}/
if you want to allow empty strings, or /{([^}]+)}/
if you want to only match when there is at least one character between the curly braces. Breakdown:
/
: start the regex pattern{
: a literal curly brace(
: start capturing[
: start defining a class of characters to capture^}
: "anything other than}
"
]
: OK, that's our whole class definition*
: any number of characters matching that class we just defined
)
: done capturing
}
: a literal curly brace must immediately follow what we captured
/
: end the regex pattern
How to match words in curly braces?
Using \w
will not match a space. You can match the content between the {{
and }}
using for example .+?
and place the capturing group between the first and the second curly brace.
In the replacement use $1
{({.+?})}
Regex demo
If you only want to match word characters with a single space between the words, you could use match 1+ word chars and repeat 0 or more times 1+ word chars preceded by a space:
{({\w+(?: \w+)*})}
Regex demo
Regex to match string between curly braces (that allows to escape them via 'doubling')
You can use
(?<!{)(?:{{)*{([^{}]*)}(?:}})*(?!})
See the .NET regex demo.
In C#, you can use
var results = Regex.Matches(text, @"(?<!{)(?:{{)*{([^{}]*)}(?:}})*(?!})").Cast<Match>().Select(x => x.Groups[1].Value).ToList();
Alternatively, to get full matches, wrap the left- and right-hand contexts in lookarounds:
(?<=(?<!{)(?:{{)*{)[^{}]*(?=}(?:}})*(?!}))
See this regex demo.
In C#:
var results = Regex.Matches(text, @"(?<=(?<!{)(?:{{)*{)[^{}]*(?=}(?:}})*(?!}))")
.Cast<Match>()
.Select(x => x.Value)
.ToList();
Regex details
(?<=(?<!{)(?:{{)*{)
- immediately to the left, there must be zero or more{{
substrings not immediately preceded with a{
char and then{
[^{}]*
- zero or more chars other than{
and}
(?=}(?:}})*(?!}))
- immediately to the right, there must be}
, zero or more}}
substrings not immediately followed with a}
char.
Regex for matching any content between curly brackets, except for single-digit numbers?
You have 2 parts of the pattern. To match what is in between except for when there is a single digit:
(?<={)(?!\d})[^{}]+(?=})
The pattern matches
(?<={)
Assert{
to the left(?!\d})
Assert not\d}
to the right[^{}]+
Match any char except{
or}
(?=})
Assert}
at the right
Regex demo
Regex match text followed by curly brackets
editor\s*{\s*(?:\"[a-z]*\"\s*\".*\"\s*)*\}
Demo
Also tested it in Notepad++ it works fine
Matching all contents between curly braces - Regex
You can use this recursive regex in PHP:
$regex = '/ { ( (?: [^{}]* | (?R) )* ) } /x';
RegEx Demo
get match of string using regex that has curly braces and replace all incl curly braces
Regular expressions should start and end with /
instead of the normal string delimiters.
let link = 'https://example.org'let content = '<span>Some text {some text inside}</span>'content = content.replace(/\{([^}]+)\}/, `<a href="${link}" target="_blank" key="anchor">some text inside</a>`)console.log(content)
regex - extract strings between curly braces without the curly braces
We can use a regex lookaround to match the {
as lookaround followed by one or more characters that are not a }
.
stringr::str_extract(text_test, '(?<=\\{)[^\\}]+')
#[1] "function_name" "function_title" "function_name"
Regex matches one or more characters that are not a }
([^\\}]+
) that follows a {
(regex lookaround ((?<=\\{)
)
In base R
, we can use regmatches/regexpr
regmatches(text_test, regexpr("(?<=\\{)[^\\}]+", text_test, perl = TRUE))
#[1] "function_name" "function_title" "function_name"
Related Topics
Rails 3: Belongs_To, Has_One and Migrations
Why Can't I Change the Value of Self
How to Sort an Alphanumeric Array in Ruby
Why Does Ruby '**' Operator Have Higher Precedence Than Unary '-'
What Is the Fastest Way to Sort a Hash
Ruby- Delete a Value from Sorted (Unique) Array at O(Log N) Runtime
Gems Not in Local Gems After Bundle Install
Openuri Causing 401 Unauthorized Error with Https Url
Actionmailer Smtp "Certificate Verify Failed"
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv3 Read Server Hello a (Openssl::Ssl::Sslerror)
Ruby on Rails Workflow Engine (Like Ibm Workflow)
How to Emit Yaml in Ruby Expanding Aliases
Rspec -- Test If Method Called Its Block Parameter
Trying to Find Vowels of a String Using Ruby While Loops
Can Someone Explain the Following Code to Me
Mini_Magick Gem Doesn't Work with My Imagemagick Install
How to Recursively Flatten a Yaml File into a JSON Object Where Keys Are Dot Separated Strings