如果一行以03:32:33(時間戳)之類的數字開頭,我正在匹配時正在寫入filebeat配置。我目前正在做 -檢查一個字符串是否以使用正則表達式的數字開頭
\d
但它沒有得到承認,有什麼我應該做的。我不是特別好/有正則表達式的經驗。幫助將不勝感激。
如果一行以03:32:33(時間戳)之類的數字開頭,我正在匹配時正在寫入filebeat配置。我目前正在做 -檢查一個字符串是否以使用正則表達式的數字開頭
\d
但它沒有得到承認,有什麼我應該做的。我不是特別好/有正則表達式的經驗。幫助將不勝感激。
真正的問題是filebeatdoes not support \d
。
替換\d
通過[0-9]
您的正則表達式將工作。
我建議你看看filebeat的Supported Patterns。
此外,請確定您使用的是^
,它代表字符串的開頭。
Regex: (^\d)
1st Capturing group (^\d)
^Match at the start of the string
\d match a digit [0-9]
您可以使用:
^\d{2}:\d{2}:\d{2}
字符^一行的開頭匹配。
你可以使用這個表達式:
^([0-9]{2}:?){3}
Assert position at the beginning of the string «^»
Match the regex below and capture its match into backreference number 1 «([0-9]{2}:?){3}»
Exactly 3 times «{3}»
You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «{3}»
Or, if you don’t want to capture anything, replace the capturing group with a non-capturing group to make your regex more efficient.
Match a single character in the range between 「0」 and 「9」 «[0-9]{2}»
Exactly 2 times «{2}»
Match the character 「:」 literally «:?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»