[英]RegEx : Pattern to match the first or only line if specific pattern is not found
I have written a RegEx pattern to match everything before the first occurrence of either a dot (.) or a comma (,). 我编写了一个RegEx模式,以匹配首次出现点(。)或逗号(,)之前的所有内容。
RegEx.Match(input, @"(.*?)(?=,|\.)").Value;
But in case a dot or comma does not occur in the input, then I would want the RegEx to return the entire string. 但是,如果输入中没有出现点或逗号,那么我希望RegEx返回整个字符串。
Example: 例:
Input : "Freeze, Police." 输入:“冻结,警察。”
Output : "Freeze" 输出:“冻结”
Input : "Aim. Fire" 输入:“目标射击”
Output : "Aim" 输出:“目标”
Input : "Roger that" 输入:“ Roger that”
Output (expected) : "Roger that" 输出(预期):“ Roger that”
Could someone suggest me how should I change my RegEx pattern ? 有人可以建议我如何更改RegEx模式吗?
Add enter marks in your regexp, 在您的正则表达式中添加输入标记,
((.|\r|\n)*?)(?=,|\.|$)
In regex "." 在正则表达式中。 does not include neither "New line" or "Carriage Return" And for end of the line use $
不包含“新行”或“回车”,对于行尾,请使用$
And of course, if you don't want to process data in multi lines then just use this. 当然,如果您不想多行处理数据,则只需使用它。
(.*?)(?=,|\.|$)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.