的正则表达式^ |在C#中 [英] Regex for ^ | in C#
问题描述
我工作的HL7消息,我需要一个正则表达式。这不起作用:
HL7消息= MSH | ^〜\&放大器; | DATACAPTOR | 123 | 123 | 20100816171948 | ORU ^ R01 | 081617194802900 | p | 2.3 | 8859/1
我的正则表达式是:
MSH | ^〜\&放大器; | DATACAPTOR | \d {3} | \d {3} |(\d { 4} \d {2} \d {2} \d {2} \d {2} \d {2})| ORU\\ ^ R01 | \d {20} | P | 2.3 | 8859/1
任何人可以提出对特殊字符的正则表达式?
我使用这个代码:
strRegex =\\vMSH | ^〜\\&放; | DATACAPTOR | \\d {3} | \\d {3} |
(\\d {4} \\d {2} \\d {2 } \\d {2} \\d {2} \\d {2})| ORU\\ ^ R01 | \\d {20} | P | 2.3 | 8859 / 1;
正则表达式RX =新的正则表达式(strRegex,RegexOptions.Compiled | RegexOptions.IgnoreCase); $ C |
$ C>,
^
和 \
是的正则表达式,所以你必须用 \
。记住 \
也是一个普通字符串中的转义字符,所以你不得不逃脱,也:
VAR strRegex =\\vMSH\\ | \\ ^〜\\\\&放大器; \\ | DATACAPTOR\\\ \\ | ...
但它通常一个更容易使用逐字字符串( @...
):
VAR strRegex = @\vMSH\ | \\ \\ ^〜\\&放大器; \ | DATACAPTOR\ | ...
最后,请注意(\d {4} \d {2} \d {2} \d {2} \d {2} \d {2})
可以简化为(\d {14})
。
不过,对于这样的结构,它可能更容易只使用 < 。code>拆分 法
VAR段=MSH | ^ 〜\&放大器; | DATACAPTOR ......;
VAR栏= segment.Split('|');
变种时间戳=字段[5];
警告:HL7消息可能会使用不同的控制字符&MDASH;启动在MSH段的第四个字符作为一个字段分隔符(在这种情况下, | ^〜\&安培;
是控制字符)。最好是先解析控制字符,如果你不控制你的输入,这些控制字符可能会改变。
I am working on HL7 messages and I need a regex. This doesn't work:
HL7 message=MSH|^~\&|DATACAPTOR|123|123|20100816171948|ORU^R01|081617194802900|P|2.3|8859/1
My regex is:
MSH|^~\&|DATACAPTOR|\d{3}|\d{3}|(\d{4}\d{2}\d{2}\d{2}\d{2}\d{2})|ORU\\^R01|\d{20}|P|2.3|8859/1
Can anybody suggest a regex for special characters? I am using this code:
strRegex = "\\vMSH|^~\\&|DATACAPTOR|\\d{3}|\\d{3}|
(\\d{4}\\d{2}\\d{2}\\d{2}\\d{2}\\d{2})|ORU\\^R01|\\d{20}|P|2.3|8859/1";
Regex rx = new Regex(strRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase );
|
, ^
, and \
are all special characters in regular expressions, so you'd have to escape them with \
. Remember \
is also an escape character within a regular string literal so you'd have to escape that, too:
var strRegex = "\\vMSH\\|\\^~\\\\&\\|DATACAPTOR\\|…
But it's generally a lot easier to use a verbatim string literal (@"…"
):
var strRegex = @"\vMSH\|\^~\\&\|DATACAPTOR\|…
Finally, note that (\d{4}\d{2}\d{2}\d{2}\d{2}\d{2})
can be simplified to (\d{14})
.
However, for a structure like this, it's probably easier to just use the Split
method.
var segment = "MSH|^~\&|DATACAPTOR…";
var fields = segment.Split('|');
var timestamp = fields[5];
Warning: HL7 messages may use different control characters—starting the 4th character in the MSH segment as a field separator (in this case |^~\&
are the control characters). It's best to parse the control characters first if you don't control your input and these control characters may change.
这篇关于的正则表达式^ |在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!