简体   繁体   English

正则表达式匹配提取

[英]Regex Match extraction

I've got some text that follows the format 我有一些遵循以下格式的文字

text ( text + numbers | text + numbers | 2-4 digit number text)

I'm interested in extracting the 2-4 digit number from this string in C#. 我对从C#中的此字符串中提取2-4位数字感兴趣。 My regEx string is 我的regEx字符串是

.*?\|.*?\|([0-9][0-9][0-9]?[0-9]?)

This correctly returns whether the string matches or not, but I'm not able to extract just the digits. 这将正确返回字符串是否匹配,但是我无法仅提取数字。

I've tried calling regex.match(input).Value, but it returns the entire input. 我试过调用regex.match(input).Value,但它会返回整个输入。

I must be missing something - any help is appreciated :) 我必须缺少一些东西-感谢您的帮助:)

Your regex defines a Group - inside parenthesis with the value you want. 您的正则表达式使用所需的值在圆括号内定义了一个Group Using just Value from the Match object returns the entire string that was matched for the regex. 仅使用Match对象的Value返回与正则表达式匹配的整个字符串。

Use regex.Match(input).Groups[1].Value for extracting the number. 使用regex.Match(input).Groups [1] .Value提取数字。

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

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