VB.Net 正则表达式 - 提取通配符值
我需要帮助从正则表达式匹配中提取通配符的值。例如:
正则表达式:“我喜欢*”
输入:“我喜欢巧克力”
我希望能够从正则表达式匹配中提取字符串“巧克力”(或不管还有什么)。如果可能的话,我还希望能够从单个通配符匹配中检索多个通配符值。例如:
正则表达式:“我演奏*和*”
输入:“我演奏吉他和贝斯”
我希望能够提取两个“吉他”和“低音”。有办法做到吗?
I need help extracting the value of a wildcard from a Regular Expressions match. For example:
Regex: "I like *"
Input: "I like chocolate"
I would like to be able to extract the string "chocolate" from the Regex match (or whatever else is there). If possible, I also want to be able to retrieve several wildcard values from a single wildcard match. For example:
Regex: "I play the * and the *"
Input: "I play the guitar and the bass"
I want to be able to extract both "guitar" and "bass". Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,正则表达式利用组的概念。组由括号表示。
所以我喜欢
我会喜欢 (.) 吗? = 所有字符 * 表示前面有多个字符或没有前面的字符
上面的代码适用于具有 I Like 的字符串,并将在包含 ' ' as 后打印出所有字符。甚至匹配空白。
你的第二种情况更有趣,因为第一个 rx 将匹配你需要更多限制的字符串的整个结尾。
I Like (\w+) 和 (\w+) :这将匹配
I Like then a space
和一个或多个单词字符,然后是and
一个空格和一个或多个单词字符
要更完整地处理正则表达式,请查看在这个网站上有一个很棒的教程。
In general regex utilize the concepts of groups. Groups are indicated by parenthesis.
So I like
Would be I like (.) . = All character * meaning as many or none of the preceding character
The above code will work for and string that has I Like and will print out all characters after including the ' ' as . matches even white space.
Your second case is more interesting because the first rx will match the entire end of the string you need something more restrictive.
I Like (\w+) and (\w+) : this will match
I Like then a space
and one or more word characters and then anand
a space andone or more word characters
For a more complete treatment of regex take a look at this site which has a great tutorial.
这是我在 VBA 中的 RegexExtract 函数。它将仅返回您指定的子匹配(仅括号中的内容)。所以在你的情况下,你会写:
这是代码。
这是一个允许您使用多个组一次提取多个部分的版本:
Here is my RegexExtract Function in VBA. It will return just the sub match you specify (only the stuff in parenthesis). So in your case, you'd write:
Here is the code.
Here is a version that will allow you to use multiple groups to extract multiple parts at once: