python - How to use ^ and $ to parse simple expression? -


how use ^ , $ symbols parse /blog/articles in following?

i've created ex3.txt contains this:

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

and regex:

^/blog/articles$ 

doesn't appear work, in when type using 'regetron' (see learning regex hard way) there no output on next line.

this exact procedure:

  1. at command line in correct directory, type: regetron ex3.txt. ex3.txt contains 1 line following:

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

although have tried newlines between entries.

  1. i type in ^/blog/article/[0-9]$ , nothing returned on next line.

  2. i try first solution posted,^\/blog\/articles$ , nothing returned.

thanks in advance soers!

based on update, sounds ^ , $ might not right operators you. match beginning , end of line respectively. if have multiple strings want match on same line, you'll need more this:

(?:^|\s)(\/blog\/articles)(?:$|\s)

what does:

(?:^|\s)            matches, not capture (?:), line start (^) or (|) whitespace (\s) (\/blog\/articles)  matches , captures /blog/articles. (?:$|\s)            matches, not capture (?:), line end ($) or (|) whitespace (\s) 

this work both cases, aware match (but not capture) single whitespace before , after /blog/articles.


Comments