RegEXPRESSION有助于非标准读取数字范围 [英] RegEXPRESSION help with non-standard reading of a number range

查看:65
本文介绍了RegEXPRESSION有助于非标准读取数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一些文本,需要查看单个句子(已经解析过)。



我想要拉数字范围。问题是,范围用破折号,带空格的破折号或简单地用到或通过一词来标识,有时在一个或两个数字上包括度数符号。在度数符号之后它也可能有一个额外的字符(它不总是标准度符号。实际数字可以是任何东西....



例如:

<前lang =HTML> 30-70°
30 - 70°
30°-70°
30°C-70°C
30°C至70°C
30°C-70°C
30至70°C



等,任何示例组合。



此外,有时度符号不符合。



句子可能还包括两个范围,例如:

 30°C-70°C(68°F-85°F)



我很难过,但是用户似乎并不明白为什么这么难。

再次,它也可能是3°到8°,它只是一个数字范围。



任何核心专家都知道从哪里开始?即使整个场景不清楚或不可行,START也会很棒。如果我能剔除低 - 挂水果我可以说服经理们我们根本无法实现自动化整个过程.....



让我补充说:它变得更加复杂:它可能会说温度高于/低于70°或者不会超过...但我真的想要它的范围。意思是,这个句子可能只有一个数字而没有范围,这个数字可能用F和C表示。帮我全能编码人.....



Buddy

解决方案

测试它:

 ' 需要引用MS VBScript正则表达式5.5  
Sub CheckDegrees()
Dim sTmp 作为 字符串,sPattern < span class =code-keyword> As String
Dim iCounter < span class =code-keyword>作为 整数
Dim oRegex < span class =code-keyword> As VBScript_RegExp_55.RegExp
Dim oMatch As VBScript_Re gExp_55.Match
Dim oMatchColl As VBScript_RegExp_55.MatchCollection

< span class =code-keyword> On 错误 GoTo Err_CheckDegrees

sTmp = 30-70°& vbCr
sTmp = sTmp& 30 - 70°& vbCrLf
sTmp = sTmp& 30°-70°& vbCrLf
sTmp = sTmp& 30°C-70°C& vbCrLf
sTmp = sTmp& 30°C至70°C& vbCrLf
sTmp = sTmp& 30°C-70°C& vbCrLf
sTmp = sTmp& 30至70°C

sPattern = (\d {1,})(°?\ s?[C | F]?)(\ s?)( - | to)(\s?)(\d {1,2})(°\ s?[C | F]?)

设置 oRegex = VBScript_RegExp_55.RegExp
使用 oRegex
.Pattern = sPattern
.MultiLine = True
全球 = True
.IgnoreCase = False
设置 oMatchColl = .Execute(sTmp)

对于 每个 oMatch oMatchColl
iCounter = iCounter + 1
MsgBox oMatch .Value& vbCr,vbInformation,iCounter
下一步
结束 使用

Exit_CheckDegrees:
开启 错误 恢复 下一步
设置 oMatchColl = Nothing
设置 oMatch = 没什么
设置 oRegex = 没什么
退出 Sub

Err_CheckDegrees:
MsgBox Err.Description,vbExclamation,Err.Number
恢复 Exit_CheckDegrees

结束


I am parsing some text and need to look within an individual sentence (already parsed).

I am looking to pull a numeric range. The problem is, the range is identified with a "dash", a "dash with spaces", or simply the word "to" or "through" and sometimes includes a degree symbol on one or both numbers. It also may have an additional character after the degree symbol (which is not always a standard degree symbol. The actual numbers can be anything....

Examples:

30-70°
30 - 70°
30°-70°
30°C-70°C
30° to 70° C
30°C-70°C
30 to 70°C


etc, any combination of the examples.

Also, sometimes the "degree" symbol is not compliant.

The sentence may also include two ranges, such as:

30°C-70°C (68°F-85°F)


I am stumped, but the users don''t seem to understand why this is so hard.
Again, it could be 3° to 8° just as well, it is just a number range.

Any hard-core experts know where to start? A START would be great even if the entire scenario is unclear or not doable. If I can cull the low-hanging fruit I can convince the managers that we simply cannot automate the entire process.....

Let me add: it get''s even more complicated: it may say Temperature above/below 70° or not to exceed... but I really want the range if it is available. Meaning, the sentence may have only ONE number and no range, and that number may be expressed in both F and C. Help me almighty coding people.....

Buddy

解决方案

Test it:

'need reference to MS VBScript Regular Expressions 5.5
Sub CheckDegrees()
Dim sTmp As String, sPattern As String
Dim iCounter As Integer
Dim oRegex As VBScript_RegExp_55.RegExp
Dim oMatch As VBScript_RegExp_55.Match
Dim oMatchColl As VBScript_RegExp_55.MatchCollection

On Error GoTo Err_CheckDegrees

sTmp = "30-70°" & vbCr
sTmp = sTmp & "30 - 70°" & vbCrLf
sTmp = sTmp & "30°-70°" & vbCrLf
sTmp = sTmp & "30°C-70°C" & vbCrLf
sTmp = sTmp & "30° to 70° C" & vbCrLf
sTmp = sTmp & "30°C-70°C" & vbCrLf
sTmp = sTmp & "30 to 70°C"

sPattern = "(\d{1,})(°?\s?[C|F]?)(\s?)(-|to)(\s?)(\d{1,2})(°\s?[C|F]?)"

Set oRegex = New VBScript_RegExp_55.RegExp
With oRegex
    .Pattern = sPattern
    .MultiLine = True
    .Global = True
    .IgnoreCase = False
    Set oMatchColl = .Execute(sTmp)
    
    For Each oMatch In oMatchColl
        iCounter = iCounter + 1
        MsgBox oMatch.Value & vbCr, vbInformation, iCounter
    Next
End With

Exit_CheckDegrees:
    On Error Resume Next
    Set oMatchColl = Nothing
    Set oMatch = Nothing
    Set oRegex = Nothing
    Exit Sub
    
Err_CheckDegrees:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_CheckDegrees

End Sub


这篇关于RegEXPRESSION有助于非标准读取数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆