python - 如何使用 ^ 和 $ 来解析简单的表达式?

标签 python regex

如何使用 ^$ 符号只解析下面的 /blog/articles

我创建了包含以下内容的 ex3.txt:

/blog/article/1 
/blog/articles 
/blog 
/admin/blog/articles

和正则表达式:

^/blog/articles$

似乎不起作用,因为当我使用“regetron”(see learning regex the hard way)键入它时,下一行没有输出。

这是我的具体步骤:

  1. 在正确目录的命令行中,我键入:regetron ex3.txt。 ex3.txt 包含一行,内容如下:

    /blog/article/1/blog/articles/blog/admin/blog/articles

尽管我尝试过在条目之间使用换行符。

  1. 我输入 ^/blog/article/[0-9]$,但下一行没有返回任何内容。

  2. 我尝试了发布的第一个解决方案,^\/blog\/articles$,但没有返回任何内容。

在此先感谢 SOers!

最佳答案

将您的正则表达式更改为:

^\/blog\/articles$

你需要转义你的斜杠。

此外,请确保 ex3.txt 文件中每一行的末尾没有尾随空格。

关于python - 如何使用 ^ 和 $ 来解析简单的表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659005/

相关文章:

python - 如何将自己的文件扩展名绑定(bind)到python脚本并抑制控制台

python - 如何从 ortools 导出 MPS 文件?

python - 错误: 'NoneType' object has no attribute 'call'

regex - 如何使用正则表达式查找并删除文件中的重复行?

python - 使用正则表达式提取字符串

regex - 通过 data.table 循环 grepl() (R)

javascript - 将破折号分隔的字符串转换为驼峰式?

python - 如何在 TensorFlow 上进行 Xavier 初始化

python - 霍夫圆检测: Blurring the image before calling hough circle algorithm?

regex - 如何编写不匹配一定量空格的正则表达式?