[英]How to check if the string contains dynamic word that starts with first letter
I am trying to find out in the string if the word that changes but starts with letter 'F' (C#).我试图在字符串中找出发生变化但以字母“F”(C#)开头的单词。 The result output from service call is as below:
服务调用的结果 output 如下:
Exception_Remote_Call--VNQ DN ERROR CODE found ERROR CODE= F0123, ERROR DESCRIPTION= NOT AVAILABLE
Exception_Remote_Call--找到 VNQ DN 错误代码 错误代码 = F0123,错误描述 = 不可用
In the above string, F0123 word changes according to the different ERROR CODE.上述字符串中, F0123字根据 ERROR CODE 的不同而变化。 I tried as below but it works for F0123 and does not work if the output is F0111.
我尝试如下,但它适用于 F0123,如果 output 是 F0111,则不起作用。 I would like to find if it starts with 'F'.
我想知道它是否以'F'开头。
var isStartsWithF = s.Contains("F0123");
I would really appreciate for the help.我非常感谢您的帮助。 Thank you in advance!
先感谢您!
This is a job for regular expressions.这是正则表达式的工作。 To make things clearer and easier to spot for future maintainers, I might include the
ERROR CODE =
as part of the expression:为了让未来的维护者更清楚、更容易发现,我可能会将
ERROR CODE =
作为表达式的一部分:
var data = "Exception_Remote_Call--VNQ DN ERROR CODE found ERROR CODE = F0123, ERROR DESCRIPTION= NOT AVAILABLE";
var exp = new Regex(@"ERROR CODE\s?= (F\d{4,5})");
var result = exp.Match(data).Groups[1].Value;
See it work here:在这里看到它的工作:
https://dotnetfiddle.net/nOXOCt
https://dotnetfiddle.net/nOXOCt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.