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) match examples

    on Mar 5, 2016

    Python regular expression module (re) can be used to check a string begins with a pattern using re.match(). Note that it is different from re.search() as it matches pattern from the beginning. Basic usage of re.match() is:

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

    Here are some examples.

    re.match – ignore case

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

    import re
    m = re.match('hello', "Hello WORLD", re.I)
    if (m):
      print m.group()
    Hello
    
    Env: Python 2.7.16

    re.match – with sub groups

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

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

    Suggested posts:

    1. Python re (regex) search examples
    2. Python re (regex) replace examples
    3. PHP – Regex OR (alternation) examples using pipe
    4. Python re search vs match
    5. Php look ahead and look behind regex examples
    6. php preg_match – greedy and lazy regex examples
    7. python print examples
    8. bash – how to use regex in if condition
    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