Julia中的正则表达式匹配 [英] regex matching in Julia

查看:74
本文介绍了Julia中的正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取文本字符串中括号内嵌入的一部分文本:

I am trying to extract a portion of text that is embedded within parenthesis in a text string:

"Dominion Diamond Corporation (DDC) "

(我要提取DDC).

细读网际网路表明正则表达式

Perusing the interwebs suggests that the regular expression

"\([^)]*\)"

会很有用.

我尝试以下操作:

ret = Regex(regExp)
match(ret, "Dominion Diamond Corporation (DDC) ")

输出:

RegexMatch("Dominion Diamond Corporation (DDC", 1="Dominion Diamond Corporation (DDC")

但是,当我将regex表达式直接输入到match函数中时:

However, when i enter the regex expression into the match function directly:

match(r"\([^)]*\)"t, "Dominion Diamond Corporation (DDC) ")

输出为:

RegexMatch("(DDC)")

为什么/这两个表达式有什么不同?如何在第一个arg中插入任意正则表达式以进行匹配?

Why / how are these two expressions different? How do I interpolate an arbitrary regex expression into the first arg for match?

推荐答案

正如@Laurel在评论中所建议的那样,单个反斜杠并未将其传递给match函数.

As @Laurel suggests in a comment, the single backslashes weren't making it through to the match function.

julia> rstring = "\\([^)]*\\)"
"\\([^)]*\\)"

julia> match(Regex(rstring), "Dominion Diamond Corporation (DDC) ")
RegexMatch("(DDC)")

这篇关于Julia中的正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆