简体   繁体   English

正则表达式,用于选择带有数字值的大括号并忽略大括号内的空格

[英]Regular Expression for selecting braces with numeric values and ignoring whitespace within the braces

I have a string which looks something like this 我有一个看起来像这样的字符串

(100) + (1000    )

I have to select both numbers along with braces ignoring the whitespace within them. 我必须选择两个数字以及大括号,而忽略其中的空白。

I was able to select (100) using the expression 我可以使用表达式选择(100)

\(-?[0-9]\d*(\.\d+)?\)

But I could not find any way to select (1000 ) where I can ignore spaces. 但是我找不到任何选择(1000)可以忽略空格的方法。

Can anyone help me out with this? 谁能帮我这个忙吗?

Just add optional spaces on both side: 只需在两侧添加可选空格:

\(\s*-?[0-9]\d*(\.\d+)?\s*\)
  ^^^                  ^^^

The following pattern should match in both cases: 在两种情况下,以下模式应匹配:

\(-?\d+(\.\d+)?\s*\)

If you want to also allow for spaces at the beginning, you could use this: 如果您还希望在开始时留有空格,则可以使用以下命令:

\(\s*-?\d+(\.\d+)?\s*\)

Note: \\d+ means the same thing as [0-9]\\d* . 注意: \\d+[0-9]\\d*含义相同。 If what you really meant was [1-9]\\d* (to disallow leading zeros), then you'd want to use that in place of the \\d+ . 如果您真正的意思是[1-9]\\d* (以防止前导零),那么您想用它代替\\d+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM