如何使用单个正则表达式匹配以“/ 1”或“/ 2”结尾的行?(How can I match lines ending with “/1” or “/2” using a single regular

编程入门 行业动态 更新时间:2024-10-22 19:23:12
如何使用单个正则表达式匹配以“/ 1”或“/ 2”结尾的行?(How can I match lines ending with “/1” or “/2” using a single regular expression?)

我正在解析文本行,并尝试使用单个正则表达式从行中删除尾部的“/ 1”或“/ 2”。

现在我有这个,它正常工作:

$id =~ s/\/1$//; $id =~ s/\/2$//;

如何将这些组合成一行? 我尝试使用大括号,但我没有让它工作。

I'm parsing lines of text and trying to remove the trailing "/1" or "/2" from the lines with a single regular expression.

Right now I have this and it works correctly:

$id =~ s/\/1$//; $id =~ s/\/2$//;

How can I combine these into a single line? I tried using braces but I haven't gotten it to work.

最满意答案

$id =~ s/\/[12]$//;

使用匹配1或2的字符类[12] 。

$id =~ s/\/[12]$//;

using the character class [12] which matches either a 1 or a 2.

更多推荐

本文发布于:2023-04-29 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   配以   结尾   正则表达式   match

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!