InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python re (regex) replace examples

    on Feb 7, 2016

    Python regular expression module can be used to replace a pattern in a string using re.sub. Basic usage of re.sub is:

    re.sub(pattern, repl, string, count=0, flags=0)
    ## if count > 0: Maximum count occurrences will be replace
    ## if count=0: All occurrences will be replaces
    

    Here are some examples.

    re.sub example using ignore case

    Replace foo with bar and use re.I (or re.IGNORECASE) flag for ignoring case.

    import re
    new = re.sub('foo', 'bar', "foo string1 FOO string2", 0, re.I)
    print new
    bar string1 bar string2
    
    Env: Python 2.7.16

    re.sub – replace whitespaces with dash

    Here we’ll replace one or more occurrences of whitespace with dash (-)

    import re
    new = re.sub('[\s]+', '-', "hello    world\n\nhello2")
    print new
    hello-world-hello2
    
    Env: Python 2.7.16

    re.sub remove c like comments using dot matches all

    Replace c like comments from a multiline string

    import re
    str = "/*line1\nline2*/\ni=0\n/*comment2*/\nj=1\n"
    new = re.sub('/\*.*?\*/', '', str, 0, re.S)
    print "===before sub ==="
    print str
    print "===after sub ==="
    print new
    ===before sub ===
    /*line1
    line2*/
    i=0
    /*comment2*/
    j=1
    
    ===after sub ===
    
    i=0
    
    j=1
    
    
    Env: Python 2.7.16
    Note that we are using greedy regex here (.*?) to avoid matching with end of second comment section.

    Suggested posts:

    1. python print examples
    2. Python/Perl/Unix one liners
    3. Python string quick start tutorial
    4. How to find python package’s file location
    5. Python re (regex) search examples
    6. Python re (regex) match examples
    7. PHP regex – whitespace shorthand (\s) regex examples
    8. Php look ahead and look behind regex examples
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python Regex, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress