[英]Regex to match number in a string enclosed within brackets or Parenthesis
I have a dataset where each line contains a number that is enclosed within a set of parenthesis or brackets.我有一个数据集,其中每行包含一个数字,该数字包含在一组括号或方括号中。 eg
例如
Jim Bob Smith [1975]
Joe Bob Public (1955)
What I'm having problems with is creating a regex expression that will match the number (without the brackets or parenthesis) that will work under both conditions.我遇到的问题是创建一个正则表达式,该表达式将匹配在两种条件下都可以使用的数字(不带括号或括号)。
I've tried我试过了
(?<=\[).+?(?=\]) and
(?<=\().+?(?=\))
So I need help finding a way to combine the two.所以我需要帮助找到一种将两者结合起来的方法。 Any assistance would be greatly appreciated.
任何帮助将不胜感激。
You may use the following .NET regex:您可以使用以下 .NET 正则表达式:
(?:(\()|\[)(.*?)(?(1)\)|])
See the regex demo查看正则表达式演示
Details细节
(?:(\\()|\\[)
- a non-capturing group that matches and captures into Group 1 a (
char, else just matches a [
char (?:(\\()|\\[)
- 一个非捕获组,匹配并捕获到组 1 a (
char, else 只匹配[
char(.*?)
- Group 2: any 0 or more chars other than a newline char, as few as possible (instead of .*?
, you might want to use \\d+
there to match 1 or more digits, or \\d{4}
to match just four digits exactly, or even (?:20|19)\\d{2}
to match a year in the 20th and 21st c.) (.*?)
- 第 2 组:除换行符以外的任何 0 个或更多字符,尽可能少(而不是.*?
,您可能想在那里使用\\d+
来匹配 1 个或多个数字,或\\d{4}
精确匹配四位数字,甚至(?:20|19)\\d{2}
匹配第 20 和 21 世纪的年份。)(?(1)\\)|])
- a conditional construct : if Group 1 was matched, a )
is matched, else, a ]
char. (?(1)\\)|])
- 条件结构:如果第 1 组匹配,则匹配 a )
,否则匹配]
字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.