英文:
Regex newline and whitespace in golang
问题
我正在尝试使用正则表达式匹配以下字符串,并从中获取一些值。
/system1/sensor37
  Targets
  Properties
    DeviceID=37-Fuse 
    ElementName=Power Supply
    OperationalStatus=Ok
    RateUnits=Celsius
    CurrentReading=49
    SensorType=Temperature
    HealthState=Ok
    oemhp_CautionValue=100
    oemhp_CriticalValue=Not Applicable
我使用了以下正则表达式:
`/system1/sensor\d\d\n.*\n.*\n\s*DeviceID=(?P<sensor>.*)\n.*\n.*\n.*\n\s*CurrentReading=(?P<reading>\d*)\n\s*SensorType=Temperature\n\s*HealthState=(?P<health>.*)\n`
现在我的问题是:有没有更好的方法来做到这一点?
我在字符串中明确提到了每个换行符和空格组。但是我可以只写/system.sensor\d\d.*DeviceID=(?P<sensor>.*)\n*.
(对我来说没有起作用,但我相信应该有一种方法可以实现。)
英文:
I was trying to match the below string with a regex and get some values out of it.
/system1/sensor37
  Targets
  Properties
    DeviceID=37-Fuse 
    ElementName=Power Supply
    OperationalStatus=Ok
    RateUnits=Celsius
    CurrentReading=49
    SensorType=Temperature
    HealthState=Ok
    oemhp_CautionValue=100
    oemhp_CriticalValue=Not Applicable
Used the below regex for that
`/system1/sensor\d\d\n.*\n.*\n\s*DeviceID=(?P<sensor>.*)\n.*\n.*\n.*\n\s*CurrentReading=(?P<reading>\d*)\n\s*SensorType=Temperature\n\s*HealthState=(?P<health>.*)\n`
Now my question is: Is there a better way to do it?
I explicitly mentioned each new line and white space group in the string. But can I just say /system.sensor\d\d.*DeviceID=(?P<sensor>.*)\n*. (It didn't work for me, but I believe there should be a way to it.)
答案1
得分: 16
默认情况下,.不匹配换行符。要改变这一点,可以使用s标志:
(?s)/system.sensor\d\d.*DeviceID=(?P<sensor>.*)
来源:RE2正则表达式语法参考
(?flags)在当前组中设置标志;非捕获组
s- 使.匹配\n(默认为false)
英文:
By default . does not match newlines. To change that, use the s flag:
(?s)/system.sensor\d\d.*DeviceID=(?P<sensor>.*)
From: RE2 regular expression syntax reference
> (?flags) set flags within current group; non-capturing
> s -  let . match \n (default false)
答案2
得分: 3
如果你想以更简洁的方式使用正则表达式来获取这些属性,首先你需要使用(?s) [在Kobi的答案中的含义和用法]。然后对于每个属性,使用以下语法:
.*ExampleProperty=(?P<example>[^\n]*).*:
.*- "忽略"开头和结尾的所有文本(匹配,但不捕获);
ExampleProperty=- 停止"忽略"文本;
(?P<example>...)- 命名捕获组;
[^\n*]- 匹配属性值直到找到换行符为止。
所以,这是一个简短的正则表达式,可以匹配你的文本并获取所有这些属性:
(?s)\/system.\/sensor\d\d.+DeviceID=(?P<sensor>[^\n]*).*CurrentReading=(?P<reading>[^\n]*).*SensorType=(?P<type>[^\n]*).*HealthState=(?P<health>[^\n]*).*
<sensor> = 37-Fuse 
<reading> = 49
<type> = Temperature
<health> = Ok
[演示]: https://regex101.com/r/Awgqpk/1
英文:
If you want to get these properties using regex in a shorter way, you'd like to firstly use (?s) [Meaning & use in Kobi's answer]. And for each property use this syntax:<br>
.*ExampleProperty=(?P<example>[^\n]*).*:
>.* - "Ignores" all text at the beginning and at the end (Match, but doesn't capture);
<br>ExampleProperty= - Stop "ignoring" the text;
<br>(?P<example>...) - Named capture group;
<br>[^\n*] - Matches the value from the property till it find a new line character.
So, this is the short regex that shall match your text and get all these properties:
(?s)\/system.\/sensor\d\d.+DeviceID=(?P<sensor>[^\n]*).*CurrentReading=(?P<reading>[^\n]*).*SensorType=(?P<type>[^\n]*).*HealthState=(?P<health>[^\n]*).*
<b>
<sensor> = 37-Fuse 
<reading> = 49
<type> = Temperature
<health> = Ok
[DEMO]: https://regex101.com/r/Awgqpk/1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论