【问题标题】:Perl matching string with mix of alphanumeric,_ and any number of square braces [closed]Perl匹配字符串,混合字母数字,_和任意数量的方括号[关闭]
【发布时间】:2017-04-04 00:30:03
【问题描述】:

如何匹配带有“字母数字字符、下划线和任意数量的开闭方括号”的字符串。 示例:
" CDN_MBIT_hresp_s_reg[0]_MB_hresp_s_reg[1]bbjabs_chiansmokrs[6] "

我试过$line=~/[a-zA-Z0-9_/[/]]/;
这似乎不起作用。

附:这个问题和Regex Matching Square Brackets很相似 但不一样 提前谢谢你。

【问题讨论】:

  • 错误的转义字符。不是/,而是\ 。试试$line=~/[a-zA-Z0-9_\[\]]/;

标签: regex perl matching


【解决方案1】:

转义时使用了错误的斜线。

/[a-zA-Z0-9_\[\]]/

或者,您可以简单地使用

/[\w\[\]]/

这两个都匹配一个字符。如果你想捕获字符串,你会想要

/([\w\[\]]+)/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多