![](/img/trans.png)
[英]in a Python regex matchobj, how to do a substring match, and assign the match to a variable?
[英]How to assign substring to a variable using regex?
我正在嘗試使用此表達式[^/]\\w{1}
從 url(例如:/en/blog/first-post/)中提取語言代碼並且它有效。 我需要的是將它分配給一個變量language_code = re.match('[^/]\\w{1}')
但是,這不起作用,因為我不想提供匹配的字符串作為爭論。 我只想從字符串中獲取兩個字母。
在 REPL:
>>> import re
>>> url_string = "/en/blog/first-post/"
>>> re.search('[^/]\w{1}', url_string)[0]
'en'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.