简体   繁体   English

我的正则表达式模式有什么问题?

[英]Whats wrong in my regex pattern?

I'm wrote a pattern 我写了一个模式

string pattern2 = @"(?[<ports>\w+,*]*)";

This pattern should help parse string of next format 此模式应有助于解析下一种格式的字符串

[port1, port2_4][portN_][port,port2,port5,p0_p1]

After parsing I'm want to have array of strings: 解析后,我想要一个字符串数组:

1. port1, port2_4
2. portN_
3. port, port2,port5,p0_p1

this will work... 这将工作...

(?<=\[)(?<ports>.*?)(?=\])
  • (?<=\\[) prefix of [ but dont include in match (?<=\\[)的前缀[但不包括在匹配
  • (?<ports>.*?) named capture "ports" match anything non greedy ( as little as possible) (?<ports>.*?)命名的捕获“端口”与任何非贪婪的匹配(尽可能少)
  • (?=\\]) suffix of ] but don't include it in the match (?=\\])的后缀] ,但不包括它在比赛中

code :- 代码:-

Regex regex = new Regex(@"(?<=\[)(?<ports>.*?)(?=\])");
var m = regex.Matches("[port1, port2_4][portN_][port,port2,port5,p0_p1]");
foreach (var p in m)
{
    Console.WriteLine(p);
}

output : 输出:

port1, port2_4
portN_
port,port2,port5,p0_p1

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

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