[英]How can i ignore a character at start/end of the match, i am like tryin to find command-line arguments
所以我想要的是分割一个空格分隔的字符串/行/文本,有点像命令行参数。 可以说我有这个
command argument0 'argument1' 'argument2, the whitespaces will be ignored'
我试过的模式:
/('[^']*')|([!-&(-~]+)/g
它匹配那些: command
argument0
'argument1'
'argument2, the whitespaces will be ignored'
它像我预期的那样工作,但我想知道是否有办法不包括'
。 如果你知道怎么做,如果可能的话,解释一下。 对不起,如果问题不清楚,这是我第一次来这里
编辑:此模式接近解决方案([^'] [^']*[^'])|([!-&(-~]+)
您应该移动第一对括号以排除'
字符并使用
'([^']*)'|([!-&(-~]+)
请参阅C# 演示:
string text = "command argument0 'argument1' 'argument2, the whitespaces will be ignored'";
string[] results = Regex.Matches(text, @"'([^']*)'|([!-&(-~]+)")
.Cast<Match>()
.Select(x => $"{x.Groups[1].Value}{x.Groups[2].Value}")
.ToArray();
Console.WriteLine(string.Join("\n", results));
输出:
command
argument0
argument1
argument2, the whitespaces will be ignored
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.