简体   繁体   中英

Extract number and text from a specific text format

I have a text similar to this:

Hello @[123456:Foo]

How to get '123456' and 'Foo' ?

My attempt:

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

You may use

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

See the regex demo . The values you need are in Group 1 and 2.

Details

  • @\\[ - a @[ substring
  • (\\d+) - Capturing group 1: one or more digits
  • : - a colon
  • ([^][]*) - Capturing group 1: zero or more chars other than ] and [
  • ] - a ] char.

C# demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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