匹配除5位数字之外的所有内容的模式
我只能访问一个可以匹配模式并用一些文本替换的函数:
Syntax
regexReplace('text', 'pattern', 'new text'
我需要以以下格式返回文本中的5个数字字符串:
CRITICAL - 192.111.6.4: rta nan, lost 100%
Created Time Tue, 5 Jul 8:45
Integration Name CheckMK Integration
Node 192.111.6.4
Metric Name POS1
Metric Value DOWN
Resource 54871
Alert Tags 54871, POS1
因此,从本文中,我想用“”除外,除了“” “ 54871”。
我提出了以下内容:
regexReplace("{{ticket.description}}", "\w*[^\d\W]\w*", "")
几乎有效,但它与符号不符。我该如何将其更改以匹配任何包含字母或符号的单词。
如您所见,我所拥有的模式非常接近,我只需要包括特殊字符和字母,而目前仅是字母:
I only have access to a function that can match a pattern and replace it with some text:
Syntax
regexReplace('text', 'pattern', 'new text'
And I need to return only the 5 digit string from text in the following format:
CRITICAL - 192.111.6.4: rta nan, lost 100%
Created Time Tue, 5 Jul 8:45
Integration Name CheckMK Integration
Node 192.111.6.4
Metric Name POS1
Metric Value DOWN
Resource 54871
Alert Tags 54871, POS1
So from this text, I want to replace everything with "" except the "54871".
I have come up with the following:
regexReplace("{{ticket.description}}", "\w*[^\d\W]\w*", "")
Which almost works but it doesn't match the symbols. How can I change this to match any word that includes a letter or symbol, essentially.
As you can see, the pattern I have is very close, I just need to include special characters and letters, whereas currently it is only letters:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将整个字符串匹配,但可以将5位数字捕获到一个捕获组中,并替换为捕获组的反向注册:
请参阅 Regex Demo 。
详细信息:
^
- 字符串的启动(?:[\ w \ w \ w]*\ s)?更多的字符,然后是Whitespace char
(\ d {5})
- 组1($ 1
包含此组模式捕获的文本):五位数字(?:\ s [\ w \ w]*)?
- Whitespace Char的可选子字符串,然后尽可能多的零或更多char。$
- 字符串结尾。You can match the whole string but capture the 5-digit number into a capturing group and replace with the backreference to the captured group:
See the regex demo.
Details:
^
- start of string(?:[\w\W]*\s)?
- an optional substring of any zero or more chars as many as possible and then a whitespace char(\d{5})
- Group 1 ($1
contains the text captured by this group pattern): five digits(?:\s[\w\W]*)?
- an optional substring of a whitespace char and then any zero or more chars as many as possible.$
- end of string.最简单的正则是:
然后,您可以用
“ $ 2”
(“ \ 2”
以其他语言中的)替换字符串,以将第二个捕获组的内容放置代码>(\ d {5})返回。唯一的问题是
。
默认情况下与newline字符不匹配。通常,您可以将标志传递到更改。
以匹配所有字符。对于大多数Regex变体,这是s
(单行)flag(pcre,java,c#,python)。其他变体使用m
(多行)标志(Ruby)。检查您正在使用的正则验证的条件变体的文档。但是,这个问题表明您无法单独传递标志,在这种情况下,您可以将其作为正则本身的一部分传递。
regex101 demo
(?s)
- 设置s
s
(单行)标志的剩余模式。哪个使。
可以匹配newline字符((?m)
ruby)。^
- 匹配字符串的开始(Ruby的\ a
)。(。*\ d)?
- [可选]匹配任何内容,然后将其存储在捕获组1中。(\ d {5})
- 匹配5位数字并将其存储在捕获组2中。(\ d。*)?
- [可选]匹配非数字,然后将其存储在捕获第3组中。$ - 匹配字符串(
\ z
Ruby)。此正则将导致存储在Capture组2中的最后5位数字。如果您想使用第一个5位数字,则必须在
(。*\ d)?。意味着它变为
(。*?\ d)?
。(?s)
受大多数Regex变体支持,但不是全部。请参阅REGEX变体文档,以查看是否适合您。javaScript的一个示例是内联标志。在这种情况下,您需要用与所有字符匹配的东西替换
。
。在javascript 可以使用。对于其他变体,这可能不起作用,您需要使用[\ s \ s]
。所有这些。假设可以使用
“ $ 2”
作为替换的语言,以及您不需要逃脱后斜切的地方,以及支持Inline(?S)
flag的Regex变体。答案是:The easiest regex is probably:
You can then replace the string with
"$2"
("\2"
in other languages) to only place the contents of the second capture group(\d{5})
back.The only issue is that
.
doesn't match newline characters by default. Normally you can pass a flag to change.
to match ALL characters. For most regex variants this is thes
(single line) flag (PCRE, Java, C#, Python). Other variants use them
(multi line) flag (Ruby). Check the documentation of the regex variant you are using for verification.However the question suggest that you're not able to pass flags separately, in which case you could pass them as part of the regex itself.
regex101 demo
(?s)
- Set thes
(single line) flag for the remainder of the pattern. Which enables.
to match newline characters ((?m)
for Ruby).^
- Match the start of the string (\A
for Ruby).(.*\D)?
- [optional] Match anything followed by a non-digit and store it in capture group 1.(\d{5})
- Match 5 digits and store it in capture group 2.(\D.*)?
- [optional] Match a non-digit followed by anything and store it in capture group 3.$
- Match the end of the string (\z
for Ruby).This regex will result in the last 5-digit number being stored in capture group 2. If you want to use the first 5-digit number instead, you'll have to use a lazy quantifier in
(.*\D)?
. Meaning that it becomes(.*?\D)?
.(?s)
is supported by most regex variants, but not all. Refer to the regex variant documentation to see if it's available for you.An example where the inline flags are not available is JavaScript. In such scenario you need to replace
.
with something that matches ALL characters. In JavaScript[^]
can be used. For other variants this might not work and you need to use[\s\S]
.With all this out of the way. Assuming a language that can use
"$2"
as replacement, and where you do not need to escape backslashes, and a regex variant that supports an inline(?s)
flag. The answer would be: