Regex在大括号“{我想要什么在花括号}之间”得到字符串

时间:2021-07-08 00:08:54

Unfortunately, despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently. This year my new year's resolution is to not try and learn regex again - So this year to save me from tears I'll give it to Stack Overflow. (Last Christmas remix).

不幸的是,尽管我试着每年至少有一年的时间学习正则表达式,但我总是忘记,因为我很少使用正则表达式。今年我的新年决心是不要再尝试和学习regex——所以今年要把我从眼泪中拯救出来,我将把它交给Stack Overflow。(去年圣诞节混音)。

I want to pass in a string in this format {getThis}, and be returned the string getThis. Could anyone be of assistance in helping to stick to my new year's resolution?

我想在这个格式{getThis}中传递一个字符串,并返回字符串getThis。有人能帮助我坚持我的新年决心吗?


Related questions on Stack Overflow:

Stack Overflow相关问题:

12 个解决方案

#1


44  

If your string will always be of that format, a regex is overkill:

如果您的字符串始终是这种格式,那么regex将被过度使用:

>>> var g='{getThis}';
>>> g.substring(1,g.length-1)
"getThis"

#2


223  

Try

试一试

/{(.*?)}/

That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.

这意味着,匹配{和}之间的任何字符,但不要贪心-匹配以}(?停止*贪婪)。圆括号让您提取匹配的部分。

Another way would be

另一个方法是

/{([^}]*)}/

This matches any character except a } char (another way of not being greedy)

这与任何字符(不贪婪的另一种方法)匹配

#3


133  

/\{([^}]+)\}/

/        - delimiter
\{       - opening literal brace escaped because it is a special character used for quantifiers eg {2,3}
(        - start capturing
[^}]     - character class consisting of
    ^    - not
    }    - a closing brace (no escaping necessary because special characters in a character class are different)
+        - one or more of the character class
)        - end capturing
\}       - the closing literal brace
/        - delimiter

#4


15  

This one works in Textmate and it matches everything in a CSS file between the curly brackets.

这个在Textmate中工作,它与CSS文件中花括号之间的所有内容相匹配。

\{(\s*?.*?)*?\}

selector {. . matches here including white space. . .}

选择器{。。这里的匹配包括空格。

If you want to further be able to return the content, then wrap it all in one more set of parentheses like so:

如果您希望进一步能够返回内容,则将其全部封装在一组括号中,如下所示:

\{((\s*?.*?)*?)\}

and you can access the contents via $1.

你可以通过$1访问内容。

This also works for functions, but I haven't tested it with nested curly brackets.

这也适用于函数,但我还没有使用嵌套的花括号来测试它。

#5


11  

Here's a simple solution using javascript replace

这里有一个使用javascript替换的简单解决方案。

var st = '{getThis}';

st = st.replace(/\{|\}/gi,''); // "getThis"

As the accepted answer above points out the original problem is easily solved with substring, but using replace can solve the more complicated use cases

正如上面所提到的,用子字符串可以很容易地解决原来的问题,但是使用replace可以解决更复杂的用例。

If you have a string like "randomstring999[fieldname]" You use a slightly different pattern to get fieldname

如果您有一个类似于“randomstring999[fieldname]”的字符串,您可以使用稍微不同的模式来获得fieldname。

var nameAttr = "randomstring999[fieldname]";

var justName = nameAttr.replace(/.*\[|\]/gi,''); // "fieldname"

#6


8  

You want to use regex lookahead and lookbehind. This will give you only what is inside the curly braces:

您希望使用regex lookahead和lookbehind。这只会给你在花括号里面的东西:

(?<=\{)(.*?)(?=\})

#7


7  

Try this:

试试这个:

/[^{\}]+(?=})/g

For example

例如

Welcome to RegExr v2.1 by #{gskinner.com},  #{ssd.sd} hosted by Media Temple!

will return gskinner.com, ssd.sd.

将返回gskinner.com ssd.sd。

#8


3  

var re = /{(.*)}/;
var m = "{helloworld}".match(re);
if (m != null)
    console.log(m[0].replace(re, '$1'));

The simpler .replace(/.*{(.*)}.*/, '$1') unfortunately returns the entire string if the regex does not match. The above code snippet can more easily detect a match.

更简单的.replace(/ * {(. *)}。*/,“$1”)不幸的是,如果regex不匹配,则返回整个字符串。上面的代码片段可以更容易地检测到匹配。

#9


3  

Try this one, according to http://www.regextester.com it works for js normaly.

试试这个吧,根据http://www.regextester.com,它适用于js规范。

([^{]*?)(?=\})

([^ {]* ?)(? = \ })

#10


2  

Regex for getting arrays of string with curly braces enclosed occurs in string, rather than just finding first occurrence.

Regex用于获取带有花括号的字符串数组,而不是只查找第一次出现。

 /\{([^}]+)\}/gm 

#11


2  

i have looked into the other answers, and a vital logic seems to be missing from them . ie, select everything between two CONSECUTIVE brackets,but NOT the brackets

我已经研究了其他的答案,其中一个重要的逻辑似乎缺失了。ie,在两个连续的方括号之间选择所有内容,但不要选择括号。

so, here is my answer

这就是我的答案。

\{([^{}]+)\}

#12


0  

You can use this regex recursion to match everythin between, even another {} (like a JSON text) :

您可以使用这个regex递归来匹配每个薄层,甚至另一个{}(如JSON文本):

\{([^()]|())*\}

#1


44  

If your string will always be of that format, a regex is overkill:

如果您的字符串始终是这种格式,那么regex将被过度使用:

>>> var g='{getThis}';
>>> g.substring(1,g.length-1)
"getThis"

#2


223  

Try

试一试

/{(.*?)}/

That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.

这意味着,匹配{和}之间的任何字符,但不要贪心-匹配以}(?停止*贪婪)。圆括号让您提取匹配的部分。

Another way would be

另一个方法是

/{([^}]*)}/

This matches any character except a } char (another way of not being greedy)

这与任何字符(不贪婪的另一种方法)匹配

#3


133  

/\{([^}]+)\}/

/        - delimiter
\{       - opening literal brace escaped because it is a special character used for quantifiers eg {2,3}
(        - start capturing
[^}]     - character class consisting of
    ^    - not
    }    - a closing brace (no escaping necessary because special characters in a character class are different)
+        - one or more of the character class
)        - end capturing
\}       - the closing literal brace
/        - delimiter

#4


15  

This one works in Textmate and it matches everything in a CSS file between the curly brackets.

这个在Textmate中工作,它与CSS文件中花括号之间的所有内容相匹配。

\{(\s*?.*?)*?\}

selector {. . matches here including white space. . .}

选择器{。。这里的匹配包括空格。

If you want to further be able to return the content, then wrap it all in one more set of parentheses like so:

如果您希望进一步能够返回内容,则将其全部封装在一组括号中,如下所示:

\{((\s*?.*?)*?)\}

and you can access the contents via $1.

你可以通过$1访问内容。

This also works for functions, but I haven't tested it with nested curly brackets.

这也适用于函数,但我还没有使用嵌套的花括号来测试它。

#5


11  

Here's a simple solution using javascript replace

这里有一个使用javascript替换的简单解决方案。

var st = '{getThis}';

st = st.replace(/\{|\}/gi,''); // "getThis"

As the accepted answer above points out the original problem is easily solved with substring, but using replace can solve the more complicated use cases

正如上面所提到的,用子字符串可以很容易地解决原来的问题,但是使用replace可以解决更复杂的用例。

If you have a string like "randomstring999[fieldname]" You use a slightly different pattern to get fieldname

如果您有一个类似于“randomstring999[fieldname]”的字符串,您可以使用稍微不同的模式来获得fieldname。

var nameAttr = "randomstring999[fieldname]";

var justName = nameAttr.replace(/.*\[|\]/gi,''); // "fieldname"

#6


8  

You want to use regex lookahead and lookbehind. This will give you only what is inside the curly braces:

您希望使用regex lookahead和lookbehind。这只会给你在花括号里面的东西:

(?<=\{)(.*?)(?=\})

#7


7  

Try this:

试试这个:

/[^{\}]+(?=})/g

For example

例如

Welcome to RegExr v2.1 by #{gskinner.com},  #{ssd.sd} hosted by Media Temple!

will return gskinner.com, ssd.sd.

将返回gskinner.com ssd.sd。

#8


3  

var re = /{(.*)}/;
var m = "{helloworld}".match(re);
if (m != null)
    console.log(m[0].replace(re, '$1'));

The simpler .replace(/.*{(.*)}.*/, '$1') unfortunately returns the entire string if the regex does not match. The above code snippet can more easily detect a match.

更简单的.replace(/ * {(. *)}。*/,“$1”)不幸的是,如果regex不匹配,则返回整个字符串。上面的代码片段可以更容易地检测到匹配。

#9


3  

Try this one, according to http://www.regextester.com it works for js normaly.

试试这个吧,根据http://www.regextester.com,它适用于js规范。

([^{]*?)(?=\})

([^ {]* ?)(? = \ })

#10


2  

Regex for getting arrays of string with curly braces enclosed occurs in string, rather than just finding first occurrence.

Regex用于获取带有花括号的字符串数组,而不是只查找第一次出现。

 /\{([^}]+)\}/gm 

#11


2  

i have looked into the other answers, and a vital logic seems to be missing from them . ie, select everything between two CONSECUTIVE brackets,but NOT the brackets

我已经研究了其他的答案,其中一个重要的逻辑似乎缺失了。ie,在两个连续的方括号之间选择所有内容,但不要选择括号。

so, here is my answer

这就是我的答案。

\{([^{}]+)\}

#12


0  

You can use this regex recursion to match everythin between, even another {} (like a JSON text) :

您可以使用这个regex递归来匹配每个薄层,甚至另一个{}(如JSON文本):

\{([^()]|())*\}