如何使用 ^
和 $
符号只解析下面的 /blog/articles
?
我创建了包含以下内容的 ex3.txt:
/blog/article/1
/blog/articles
/blog
/admin/blog/articles
和正则表达式:
^/blog/articles$
似乎不起作用,因为当我使用“regetron”(see learning regex the hard way)键入它时,下一行没有输出。
这是我的具体步骤:
在正确目录的命令行中,我键入:regetron ex3.txt。 ex3.txt 包含一行,内容如下:
/blog/article/1/blog/articles/blog/admin/blog/articles
尽管我尝试过在条目之间使用换行符。
我输入
^/blog/article/[0-9]$
,但下一行没有返回任何内容。我尝试了发布的第一个解决方案,
^\/blog\/articles$
,但没有返回任何内容。
在此先感谢 SOers!
最佳答案
将您的正则表达式更改为:
^\/blog\/articles$
你需要转义你的斜杠。
此外,请确保 ex3.txt
文件中每一行的末尾没有尾随空格。
关于python - 如何使用 ^ 和 $ 来解析简单的表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659005/