【问题标题】:Match returning capture but not pattern [duplicate]匹配返回捕获但不匹配模式[重复]
【发布时间】:2019-02-22 12:08:18
【问题描述】:

这里有个问题:

Regex Match.Value returning entire value, not the matched groups

但我的问题不同

我用

var match = _regex.Match(inputString);
return match.Value;

而且我想在不按索引引用组的情况下进行处理。有可能吗?

我想要一个不返回的匹配项 #WORD 来自字符串#WORD "SOMEWORD"

模式:^#WORD(.+)$

输入:#WORD "SOMEWORD"

返回#WORD "SOMEWORD"

我只需要"SOMEWORD"

【问题讨论】:

  • 您可以将您的捕获组转换为命名(?<word>.+) 并使用this 回答。或者只是按索引(在您的情况下为 1)引用您的组
  • 您已经捕获了该部分,使用 match.Groups[1].Value 访问它,或者 - 如果您需要修剪它 - match.Groups[1].Value.Trim()

标签: c# regex regex-group


【解决方案1】:

你可以使用

(?<=^#WORD ")\w+(?="$)

Regexr

【讨论】:

  • 你的意思是?&lt;=?! 表面上会起作用,但不是出于正确的原因。
  • 不工作,可以放任何字符串而不是WORD
  • 对不起,我打错了。现在已修复。你能再试一次吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2021-11-26
相关资源
最近更新 更多