InfoHeap
Tech
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) search examples

    on Feb 6, 2016

    Python regular expression module (re) can be used to find presence of a pattern in a string using re.search. Basic usage of re.search is:

    re.search(pattern, string, flags=0)
    

    Here are some examples.

    re.search – ignore case

    We can use flag re.I (or re.IGNORECASE) for ignoring case.

    import re
    m = re.search('world', "Hello WORLD", re.I)
    if (m):
      print m.group()
    WORLD
    
    Env: Python 2.7.18

    re.search – with sub groups

    Round brackets in pattern can be used for subgroups matching and can be accessed using m.groups().

    import re
    m = re.search('(hello)\s+(world)', "a Hello WORLD", re.I)
    if (m):
      print m.group()
      print m.groups()
    Hello WORLD
    ('Hello', 'WORLD')
    
    Env: Python 2.7.18

    re.search – dot matches all

    We can use re.S (or re.DOTALL) to match dot with all characters including newline.

    import re
    m = re.search('hello.*world', "Hello WORLD\nHello world.", re.I)
    print "== when not using re.S =="
    if (m):
      print m.group()
    m = re.search('hello.*world', "Hello WORLD\nHello world.", re.I|re.S)
    print "== when using re.S =="
    if (m):
      print m.group()
    == when not using re.S ==
    Hello WORLD
    == when using re.S ==
    Hello WORLD
    Hello world
    
    Env: Python 2.7.18

    Suggested posts:

    1. CSS max-height – limit maximum height of an element
    2. Python re (regex) match examples
    3. Python selenium – execute javascript code
    4. Javascript/jQuery – disable right click
    5. Python for loop examples
    6. Bash – how to compare file timestamps
    7. PHP – convert dos newline to unix format
    8. Css source map example using gulp-sourcemaps
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python Regex, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | 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 | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress