I am trying to match some pattern, reference this online tool for easy test
pattern = r"(^/\w+)\s*?(\w+)"
string_1 = "/path_one path_two"
string_2 = "/path_one_only"
While string_1 matches as expected by returning both parts, for string_2 it cuts the last character of part one. But I would like the pattern to always return both parts, rather return None/empty string if path two does not exist
Use this pattern:
pattern = r"(^/\w+)\s*(\w*)"
Now the second string will match entirely in the first capture group.
The reason the final character was being clipped in the second string can be seen here:
(^/\w+) - matches 'path_one_onl'
\s*? - matches nothing (there are no spaces)
(\w+) - matches 'y'
In other words, the second capture group was imposing that at least one character be matched there.
Change your pattern string to
either
pattern = r"(^/\\w+)\\s*(\\w+)?"
or
pattern = r"(^/\\w+)\\s*(\\w*)"
You do not need \\s*?
, just \\s*
is fine.
您还可以通过在其周围添加一个非捕获组来使整个第二部分(包括空格)成为可选的:
r"(^/\w+)(?:\s+(\w+))?"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.