python
  • regex
  • 2016-04-30 55 views 1 likes 
    1

    我有一個Python regexdebuggex精細工作:Python的正則表達式與回顧後和前瞻工作不

    enter image description here

    然而,當我這樣做在Python控制檯:

    import re 
    rgx = re.compile(r'(?<="careerJobAdsList",){"jobAds":.*}](?=,"nodes":)') 
    
    st = 'widget("careerJobAdsList", {"jobAds":[{"id":607}],"nodes":[{"id":2,"parent_id"' 
    
    rgx.match(st) 
    >>> None 
    

    我我試圖逃過rgx中的所有特殊字符,但它不會改變輸出。

    我錯過了什麼嗎?

    回答

    3

    match找到匹配字符串的開頭。使用search代替

    print(rgx.search(st)) 
    

    報價re.match

    如果在字符串的開頭零個或多個字符匹配 正則表達式模式,返回相應的MatchObject 實例。如果字符串與模式不匹配,則返回None;注意 這與零長度匹配不同。

    Python代碼

    import re 
    rgx = re.compile(r'(?<="careerJobAdsList",){"jobAds":.*}](?=,"nodes":)') 
    st = 'widget("careerJobAdsList", {"jobAds":[{"id":607}],"nodes":[{"id":2,"parent_id"' 
    print(rgx.search(st)) 
    
    +1

    是我不好......我總是忘了'match'僅僅是個開始匹配...感謝 – Jivan

    +0

    @Jivan它發生.. – rock321987

    相關問題