眼科折射的正则表达式

时间:2016-06-08 13:09:11

标签: .net regex vb.net

我试图创建一个与眼科学中使用的特定数据集匹配的正则表达式(.net),称为折射。此匹配的目的是从诊断设备读取串行数据并进行相应的解析。当存在缓冲区错误或仅收到部分读取时,我需要尝试解决问题。

折射记录在3个部分中:Sph,Cyl和Axis。 SPH可以是正值或负值(总是用前面的+/-表示),通常写为##。##或#。##。 cyl值可以再次为正或负(始终使用前面的+/-),并再次使用相同的##。## /#。##格式。最后一个轴值必须在0到360之间。

匹配值示例:

+ 0.00 + 0.50 74
+0.00+0.50x74
+1.25 -2.20 110
+14.50 -0.00 96

示例不匹配值:

从类似机器使用的常用表达式,它们将输出不应导致匹配的数据。

 39.50  8.55 171
40.25  8.35 80
+ 0.50 DK 90
+ 1.25 125
+ 0.25* 109

的问题:

  • 解析值时,+ / - (或不)

  • 之间可能有空格
  • 最后两组数字之间可能有多个空格,或者某些系统会以#x#格式输出。

  • 第一和第二个数字集可以是#。##格式;或##。##格式,可能包括也可能不包括前导0

到目前为止我所拥有的

[+-]+\s*([0-9]{1,2})+\.+([0-9]{1,2})+\s*[+-]\s*[0-9]{1,2}\.[0-9]{1,2}\s*[0-9]{1,3}
我似乎无比复杂。我也没有考虑x限定符。

编辑:终端数据转储 为什么数据在某些情况下是不同的,我不确定,我只是看到不同的样本数据;相当旧的机器。

.
.HARK    SEQ   2
.
.Sph    Cyl    Axis    VA
RIGHT EYEObj
.+ 0.00 + 0.50  74
.
.Reflex:  58

.LEFT EYEObj
.+ 0.50 + 0.25 109
.
.Reflex:  34
.Vertex:  12.0
PD:      56
.
.Ker   DK     MM    Axis
R   39.50   8.55   171
40.25   8.37    81

. AVG.    40.00  8.46
K  + 0.75 DK x 81

.L   39.00   8.66   177
.    39.50   8.52    87
.
. AVG.    39.25  8.59
.    K  + 0.50 DK x 87
..

4 个答案:

答案 0 :(得分:0)

以下正则表达式与您给出的样本模式相匹配,但是我认为您最好将模式匹配分解为3个表达式而不是整体结构,如果用于其他任何方式来减轻a的影响从新系统添加新模式。

[+-]{1}\s{0,1}[0-9.]+\s{0,1}[+-]{1}\s{0,1}[0-9.]+[\sx]{0,1}[0-9]+

这里是something you can fiddle with

答案 1 :(得分:0)

最后一个数字可以在0到360之间的条件很棘手:)

无论如何,我测试了你的测试用例,这似乎有效:^[+-]\s*[\d]{1,2}\.[\d]{2}\s*[+-]\s*[\d]{1,2}\.[\d]{2}(?:x|\s*)(?:[\d]|[1-9][\d]|[1-2][\d][\d]|3[0-5][\d]|360)$

这里的工作示例:https://regex101.com/r/mH3zQ0/2

答案 2 :(得分:0)

这匹配了所有您想要的商品,没有任何不受欢迎的商品。试一试:

"[\+\-]\s*\d+\.\d+\s*[\+\-]\s*\d+\.\d+[\sx]+(\d{1,2}|[12]\d\d|3[012345]\d|360)\b"

答案 3 :(得分:0)

以下正则表达式正确匹配所有测试用例。它以自由间距格式呈现,带有大量注释,因此它可以自我记录:

if (Regex.IsMatch(subjectString, 
    @"(?#!cs ophthalmology_refraction 20160608_0800)
    # SPH, CYL and AXIS Ophthalmology Refraction - e.g. +0.00+0.50x74
    ^            # Anchor to start of line.
    [ \t]*       # Optional whitespace (ws) before SPH sign.
    [+\-]        # Required sign for SPH.
    [ \t]*       # Optional ws before SPH value.
    \d{1,2}      # SPH value integer part.
    \.           # Required SPH decimal point.
    \d{2}        # SPH value fractional part has 2 digits.
    [ \t]*       # Optional whitespace (ws) before CYL sign.
    [+\-]        # Required sign for CYL.
    [ \t]*       # Optional ws before CYL value.
    \d{1,2}      # CYL value integer part.
    \.           # Required CYL decimal point.
    \d{2}        # CYL value fractional part has 2 digits.
    (?:          # Separator between CYL and AXIS - either space or x.
      [ \t]+     # Either one or more ws,
    | [Xx]       # Or an upper or lowercase X.
    )            # End separator between CYL and AXIS.
    (?:          # AXIS value is an integer from 0 to 360.
      360        # Either 360.
    | 3[0-5]\d   # Or 300-359.
    | [1-2]\d\d  # Or  100-299.
    | \d\d?      # Or 0-99.
    )            # End AXIS value.
    [ \t]*       # Optional ws after AXIS value.
    $            # Anchor to end of line.", 
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)) {
    // Successful match
} else {
    // Match attempt failed
} 

请注意,原始问题未明确定义出现这些值的上下文。此解决方案假定每个三值数据集出现在一行上,并且值之间允许的空格可以是空格或制表符。