正则表达式中的换行符和空白符在 Golang 中的表示方法是什么?

huangapple go评论88阅读模式
英文:

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&lt;sensor&gt;.*)\n.*\n.*\n.*\n\s*CurrentReading=(?P&lt;reading&gt;\d*)\n\s*SensorType=Temperature\n\s*HealthState=(?P&lt;health&gt;.*)\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&lt;sensor&gt;.*)\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&lt;sensor&gt;.*)

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&lt;example&gt;[^\n]*).*:
>.* - "Ignores" all text at the beginning and at the end (Match, but doesn't capture);
<br>ExampleProperty= - Stop "ignoring" the text;
<br>(?P&lt;example&gt;...) - 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&lt;sensor&gt;[^\n]*).*CurrentReading=(?P&lt;reading&gt;[^\n]*).*SensorType=(?P&lt;type&gt;[^\n]*).*HealthState=(?P&lt;health&gt;[^\n]*).*

<b>

&lt;sensor&gt; = 37-Fuse 
&lt;reading&gt; = 49
&lt;type&gt; = Temperature
&lt;health&gt; = Ok

[DEMO]: https://regex101.com/r/Awgqpk/1

huangapple
  • 本文由 发表于 2017年4月30日 20:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/43706322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定