Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将方括号内的数字与可选文本匹配的正则表达式_C#_Regex - Fatal编程技术网

C# 将方括号内的数字与可选文本匹配的正则表达式

C# 将方括号内的数字与可选文本匹配的正则表达式,c#,regex,C#,Regex,首先,我在这里,所以这就是我正在处理的正则表达式的风格。以下是我需要匹配的东西: [(1)] 或 所以基本上我需要知道括号之间的内容是否是数字,忽略右括号和右方括号之间的所有内容。任何正则表达式大师愿意帮忙吗?这应该可以: \[\(\d+\).*?\] 如果需要捕捉数字,只需将\d+括在括号中: \[\((\d+)\).*?\] 比如: \[\(\d+\)[^\]]*\] 可能还需要一些转义?您必须匹配[]吗?你能做的只是 \((\d+)\) (数字本身将分组) 例如 var mg =

首先,我在这里,所以这就是我正在处理的正则表达式的风格。以下是我需要匹配的东西:

[(1)]

所以基本上我需要知道括号之间的内容是否是数字,忽略右括号和右方括号之间的所有内容。任何正则表达式大师愿意帮忙吗?

这应该可以:

\[\(\d+\).*?\]
如果需要捕捉数字,只需将
\d+
括在括号中:

\[\((\d+)\).*?\]
比如:

\[\(\d+\)[^\]]*\]

可能还需要一些转义?

您必须匹配[]吗?你能做的只是

\((\d+)\)
(数字本身将分组)

例如

var mg = Regex.Match( "[(34) Some Text - Some Other Text]", @"\((\d+)\)");

if (mg.Success)
{
  var num = mg.Groups[1].Value; // num == 34
}
  else
{
  // No match
}

怎么样“^\[\((d+)”(perl风格,不熟悉C)。我认为您可以放心地忽略这行的其余部分。

取决于您试图完成的任务

List<Boolean> rslt;
String searchIn;
Regex regxObj;
MatchCollection mtchObj;
Int32 mtchGrp;

searchIn = @"[(34) Some Text - Some Other Text] [(1)]";

regxObj = new Regex(@"\[\(([^\)]+)\)[^\]]*\]");

mtchObj = regxObj.Matches(searchIn);

if (mtchObj.Count > 0)
    rslt = new List<bool>(mtchObj.Count);
else
    rslt = new List<bool>();

foreach (Match crntMtch in mtchObj)
{
    if (Int32.TryParse(crntMtch.Value, out mtchGrp))
    {
        rslt.Add(true);
    }
}
列出rslt;
字符串搜索;
Regex regxObj;
匹配集合mtchObj;
Int32 mtchGrp;
searchIn=@“[(34)一些文本-一些其他文本][(1)]”;
regxObj=newregex(@“\[\(([^\)]+)\[^\]]*\]”);
mtchObj=regxObj.Matches(searchIn);
如果(mtchObj.Count>0)
rslt=新列表(mtchObj.Count);
其他的
rslt=新列表();
foreach(匹配mtchObj中的crntMtch)
{
if(Int32.TryParse(crntMtch.Value,out mtchGrp))
{
rslt.Add(真);
}
}

这是怎么回事?假设您只需要确定字符串是否匹配,而不需要提取数值

        string test = "[(34) Some Text - Some Other Text]";

        Regex regex = new Regex( "\\[\\(\\d+\\).*\\]" );

        Match match = regex.Match( test );

        Console.WriteLine( "{0}\t{1}", test, match.Success );

在这种情况下,正则表达式似乎有些矫枉过正。这是我最终使用的解决方案

var src = test.IndexOf('(') + 1;
var dst = test.IndexOf(')') - 1;
var result = test.SubString(src, dst-src);

1线对3线不是我所说的过度杀戮。
var src = test.IndexOf('(') + 1;
var dst = test.IndexOf(')') - 1;
var result = test.SubString(src, dst-src);