正则表达式以匹配两个字符之间的多个数字组 [英] Regex to match multiple number groups between two characters
问题描述
我有一个类似于以下内容的字符串:
I have a string that looks like the following:
<@399969178745962506> hello to <@!104729417217032192>
我有一本包含两者的字典,如下所示:
I have a dictionary containing both that looks like following:
{"399969178745962506", "One"},
{"104729417217032192", "Two"}
我的目标是将< @ 399969178745962506>
替换为该数字键的值,本例中为 One
My goal here is to replace the <@399969178745962506>
into the value of that number key, which in this case would be One
Regex.Replace(arg.Content, "(?<=<)(.*?)(?=>)", m => userDic.ContainsKey(m.Value) ? userDic[m.Value] : m.Value);
我当前的正则表达式如下:(?< =<)(.*?)(?=>)
仅匹配<
和>
,在这种情况下将同时保留 @ 399969178745962506
和 @ !! 104729417217032192
My current regex is as following: (?<=<)(.*?)(?=>)
which only matches everything in between <
and >
which would in this case leave both @399969178745962506
and @!104729417217032192
我不能只是忽略 @
符号,因为每次都不存在!
符号.因此,最好只获取具有 \ d +
I can't just ignore the @
sign, because the !
sign is not there every time. So it could be optimal to only get numbers with something like \d+
我需要弄清楚如何只获取<
和>
之间的数字,但我一生都无法弄清楚如何实现.
I need to figure out how to only get the numbers between <
and >
but I can't for the life of me figure out how.
非常感谢您的帮助!
推荐答案
要从给定格式中仅提取数字,请使用以下正则表达式模式:
To extract just the numbers from you're given format, use this regex pattern:
(?<=<@|<@!)(\d+)(?=>)
查看它的实际效果: https://regexr.com/3j6ia
这篇关于正则表达式以匹配两个字符之间的多个数字组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!