[英]Extract number after specific word and first open parenthesis using regex in C#
i want to get a string from a sentence which starts with a word Id:我想从一个以单词 ID 开头的句子中获取一个字符串:
Letter received for the claim Id: Sanjay Kumar (12345678 / NA123456789) Dear Customer find the report收到的索赔信件 ID:Sanjay Kumar (12345678 / NA123456789) 尊敬的客户 查找报告
op: Id: Sanjay Kumar (12345678 / NA123456789)操作:身份证:Sanjay Kumar (12345678 / NA123456789)
Exp op: 12345678经验值:12345678
Code代码
var regex = new Regex(@"[\n\r].*Id:\s*([^\n\r]*)");
var useridText = regex.Match(extractedDocContent).Value;
You can use您可以使用
var regex = new Regex(@"(?<=Id:[^()]*\()\d+");
See the regex demo .请参阅正则表达式演示。
Details :详情:
(?<=Id:[^()]*\()
- a positive lookbehind that matches a location that is immediately preceded with Id:
+ zero or more chars other than (
and )
+ (
(?<=Id:[^()]*\()
- 与紧接在Id:
+ 除(
和)
+ (
之外的零个或多个字符\d+
- one or more digtis. \d+
- 一个或多个数字。Consider also a non-lookbehind approach:还考虑一种非后视方法:
var pattern = @"Id:[^()]*\((\d+)";
var useridText = Regex.Match(extractedDocContent, pattern)?.Groups[1].Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.