python re.sub

來源:互聯網
上載者:User

標籤:pattern   正則   字串   name   def   locale   python re   most   return   

1.

  

re.sub?Signature: re.sub(pattern, repl, string, count=0, flags=0)Docstring:Return the string obtained by replacing the leftmostnon-overlapping occurrences of the pattern in string by thereplacement repl.  repl can be either a string or a callable;if a string, backslash escapes in it are processed.  If it isa callable, it‘s passed the match object and must returna replacement string to be used.

 

  參數說明:pattern模式字串,可以數字命名也可以name命名(\g<1>==\1)(?P<name>----------------\g<name>)

        repl 替換的字串也可以是函數  string源串

        count替換的次數 

      flag的值為:

re.I    使匹配對大小寫不敏感re.L    做本地化識別(locale-aware)匹配re.M    多行匹配,影響^和$re.S    使.匹配包括換行在內的所有字元re.U    根據Unicode字元集解析字元。這個標誌影響\w、\W、 \b和\Bre.X    該標誌通過給予你更靈活的格式以便你將Regex寫得更易於理解

2.執行個體

  

def replace_digit(m):    ss = u‘〇一二三四五六七八九‘    index = int(m.group())    return ss[index]s = u‘1990年3月27日‘result = re.sub(u‘\d‘, replace_digit, s, count=4)print(result) # 一九九〇年3月27日

 

s = ‘2017-01-22‘s = re.sub(‘(\d{4})-(\d{2})-(\d{2})‘, r‘\2-\3-\1‘, s)print(s) # 01-22-2017

 

  

python re.sub

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.