简体   繁体   English

从特定的文本格式中提取数字和文本

[英]Extract number and text from a specific text format

I have a text similar to this: 我有类似这样的文字:

Hello @[123456:Foo] 您好@ [123456:Foo]

How to get '123456' and 'Foo' ? 如何获得“ 123456”和“ Foo”?

My attempt: 我的尝试:

@\[([\d+]):([\s+])\]

You may use 您可以使用

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

See the regex demo . 参见regex演示 The values you need are in Group 1 and 2. 您需要的值在组1和2中。

Details 细节

  • @\\[ - a @[ substring @\\[ -一个@[子字符串
  • (\\d+) - Capturing group 1: one or more digits (\\d+) -捕获组1:一位或多位数字
  • : - a colon : -冒号
  • ([^][]*) - Capturing group 1: zero or more chars other than ] and [ ([^][]*) -捕获组1:零个或多个除[ ][
  • ] - a ] char. ] -一个]字符。

C# demo: C#演示:

var m = Regex.Match(str, @"@\[(\d+):([^][]*)]");
if (m.Success) 
{
    Console.WriteLine(m.Groups[1].Value);
    Console.WriteLine(m.Groups[2].Value);
}

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

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