{"id":3090,"date":"2023-10-19T12:07:39","date_gmt":"2023-10-19T06:37:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=3090"},"modified":"2023-11-10T11:48:07","modified_gmt":"2023-11-10T06:18:07","slug":"basics-of-python-regular-expression-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/","title":{"rendered":"Basics of Python \u2013 Regular Expression Module"},"content":{"rendered":"

In this Page, We are Providing Basics of Python \u2013 Regular Expression Module. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Basics of Python \u2013 Regular Expression Module<\/h2>\n

Regular expression module<\/strong><\/p>\n

A regular expression (also called RE, or regex, or regex pattern) is a specialized approach in Python, using which programmers can specify rules for the set of possible strings that need to be matched; this set might contain English sentences, e-mail addresses, or anything. REs can also be used to modify a string or to split it apart in various ways.<\/p>\n

Meta characters<\/strong><\/p>\n

Most letters and characters will simply match themselves. For example, the regular expression test will match the string test exactly. There are exceptions to this rule; some characters are special “meta characters”, and do not match themselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affect other portions of the RE by repeating them or changing their meaning. Some of the meta characters are discussed below:<\/p>\n\n\n\n\n\n\n\n\n\n\n
\n

Meta character<\/p>\n<\/td>\n

Description<\/td>\n\n

Example<\/p>\n<\/td>\n<\/tr>\n

\n

[ ]<\/p>\n<\/td>\n

\n

Used to match a set of characters.<\/p>\n<\/td>\n

\n

[time]<\/p>\n

The regular expression would match any of the characters t, i, m, or e.<\/p>\n

[a-z]<\/p>\n

The regular expression would match only lowercase characters.<\/p>\n<\/td>\n<\/tr>\n

\n

\u2227<\/p>\n<\/td>\n

Used to complement a set of characters.<\/td>\n[\u2227<\/sup>time]<\/p>\n

The regular expression would match any other characters than t, i, m or e.<\/td>\n<\/tr>\n

\n

$<\/p>\n<\/td>\n

Used to match the end of string only.<\/td>\ntime$<\/p>\n

The regular expression would match time in ontime, but will not match time in timetable.<\/td>\n<\/tr>\n

\n

*<\/p>\n<\/td>\n

Used to specify that the previous character can be matched zero or more times.<\/td>\ntim*e<\/p>\n

The regular expression would match strings like timme, tie and so on.<\/td>\n<\/tr>\n

\n

+<\/p>\n<\/td>\n

Used to specify that the previous character can be matched one or more times.<\/td>\ntim+e<\/p>\n

The regular expression would match strings like timme, timmme, time and so on.<\/td>\n<\/tr>\n

\n

?<\/p>\n<\/td>\n

Used to specify that the previous character can be matched either once or zero times.<\/td>\ntim ?e<\/p>\n

The regular expression would only match strings like time or tie.<\/td>\n<\/tr>\n

\n

{ }<\/p>\n<\/td>\n

The curly brackets accept two integer values. The first value specifies the minimum number of occurrences and the second value specifies the maximum of occurrences.<\/td>\ntim{1,4}e<\/p>\n

The regular expression would match only strings time, timme, timmme or timmmme.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Regular expression module functions<\/strong><\/p>\n

Some of the methods of re module as discussed below:<\/p>\n

re. compile ( pattern )
\nThe function compile a regular expression pattern into a regular expression object, which can be used for matching using its match ( ) and search ( ) methods, discussed below.<\/p>\n

>>> import re\r\n>>> p=re.compile ( ' tim*e ' )<\/pre>\n

re. match ( pattern, string )
\nIf zero or more characters at the beginning of the string match the regular expression pattern, match-( ) return a corresponding match object instance. The function returns None if the string does not match the pattern.<\/p>\n

re. group ( )
\nThe function return the string matched by the RE.<\/p>\n

>>> m=re .match ( ' tim*e' , ' timme pass time ' )\r\n>>> m. group ( )\r\n' timme '<\/pre>\n

The above patch of code can also be written as:<\/p>\n

>>> p=re. compile ( ' tim*e ' )\r\n>>> m=p.match ( ' timme pass timme ' )\r\n>>> m.group ( )\r\n'timme'<\/pre>\n

re. search ( pattern, string )
\nThe function scans through string looking for a location where the regular expression pattern produces a match, and returns a corresponding match object instance. The function returns None if no position in the string matches the pattern.<\/p>\n

>>> m=re.search( ' tim*e ' ' no passtimmmeee ' )\r\n>>> m.group ( )\r\n' timmme '<\/pre>\n

The above patch of code can also be written as:<\/p>\n

>>> p=re.compile ( ' tim*e ' )\r\n>>> m=p.search ( ' no passtimmmeee ' )\r\n>>> m.group ( )\r\n' timmme '<\/pre>\n

re.start ( )
\nThe function returns the starting position of the match.<\/p>\n

re.end ( )
\nThe function returns the end position of the match.<\/p>\n

re.span ( )
\nThe function returns a tuple containing the ( start, end ) indexes of the match.<\/p>\n

>>> m=re.search ( ' tim*eno passtimmmeee ' )\r\n>>> m.start ( )\r\n7\r\n>>> m.end ( )\r\n13\r\n>>> m.span ( )\r\n( 7 , 13 )<\/pre>\n

The above patch of code can also be written as:<\/p>\n

>>> p=re.compile ( ' tim*e ' )\r\n>>> m=p.search ( ' no passtimmmeee ' )\r\n>>> m.start ( )\r\n7 \r\n>>> m.end ( )\r\n13\r\n>>> m.span ( )\r\n( 7 , 13 )<\/pre>\n

re. findall ( pattern, string )
\nThe function returns all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.<\/p>\n

>>> m=re.findall ( ' tim*e ' , ' timeee break no pass timmmeee ' )\r\n>>> m \r\n[ ' time ' , ' timmme ' ]<\/pre>\n

The above patch of code can also be written as:<\/p>\n

>>> p=re . compile ( ' tim*e ' )\r\n>>> m=p.findall ( ' timeee break no pass timmmeee ' )\r\n>>> m\r\n[ ' time ', ' timmme ' ]<\/pre>\n

re. finditer ( pattern, string )
\nThe function returns an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in a string. The string is scanned left-to-right, and matches are returned in the order found.<\/p>\n

>>> m=re.finditer ( ' tim*e ', ' timeee break no pass timmmeee ' )\r\n>>> for match in m :\r\n. . .\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0print match.group ( )\r\n. . .\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0print match.span ( )\r\ntime \r\n( 0 , 4 ) \r\ntimmme \r\n( 21 , 27 )<\/pre>\n

The above patch of code can also be written as:<\/p>\n

>>> p=re.compile( ' tim*e ' )\r\n>>> m=p.finditer ( ' timeee break no pass timmmeee ' )\r\n>>> for match in m :\r\n. . .         print match.group ( )\r\n. . .         print match.span ( )\r\ntime \r\n( 0 , 4 ) \r\ntimmme\r\n( 21 , 27 )<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Basics of Python \u2013 Regular Expression Module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Basics of Python \u2013 Regular Expression Module Regular expression module A regular expression (also called RE, or regex, or regex pattern) is a specialized approach in Python, using which programmers …<\/p>\n

Basics of Python \u2013 Regular Expression Module<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nBasics of Python \u2013 Regular Expression Module - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basics of Python \u2013 Regular Expression Module - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Basics of Python \u2013 Regular Expression Module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Basics of Python \u2013 Regular Expression Module Regular expression module A regular expression (also called RE, or regex, or regex pattern) is a specialized approach in Python, using which programmers … Basics of Python \u2013 Regular Expression Module Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-19T06:37:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:18:07+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage\",\"url\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/\",\"name\":\"Basics of Python \u2013 Regular Expression Module - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-19T06:37:39+00:00\",\"dateModified\":\"2023-11-10T06:18:07+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basics of Python \u2013 Regular Expression Module\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Basics of Python \u2013 Regular Expression Module\",\"datePublished\":\"2023-10-19T06:37:39+00:00\",\"dateModified\":\"2023-11-10T06:18:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage\"},\"wordCount\":667,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Basics of Python \u2013 Regular Expression Module - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/","og_locale":"en_US","og_type":"article","og_title":"Basics of Python \u2013 Regular Expression Module - Python Programs","og_description":"In this Page, We are Providing Basics of Python \u2013 Regular Expression Module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Basics of Python \u2013 Regular Expression Module Regular expression module A regular expression (also called RE, or regex, or regex pattern) is a specialized approach in Python, using which programmers … Basics of Python \u2013 Regular Expression Module Read More »","og_url":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-19T06:37:39+00:00","article_modified_time":"2023-11-10T06:18:07+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage","url":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/","name":"Basics of Python \u2013 Regular Expression Module - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-19T06:37:39+00:00","dateModified":"2023-11-10T06:18:07+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Basics of Python \u2013 Regular Expression Module"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Basics of Python \u2013 Regular Expression Module","datePublished":"2023-10-19T06:37:39+00:00","dateModified":"2023-11-10T06:18:07+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#webpage"},"wordCount":667,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/basics-of-python-regular-expression-module\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3090"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3090"}],"version-history":[{"count":5,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3090\/revisions"}],"predecessor-version":[{"id":3114,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3090\/revisions\/3114"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}