Change language

# 5 Python RegEx metacharacters | Python Advanced Tutorial

# 5 Python RegEx metacharacters | Python Advanced Tutorial

in this video you will be learning about metacharacters used to write regular expression what are meta characters? metacharacters are characters with special mean ing we use meta characters in regular expression to define search pattern you can note here these are some of the symbols these symbols have special mean ing when we are using it with regular expression.

lets understand these metacharacters with simple example.

import re module i am defining a pattern this patern contains three special symbols carot symbol dollar symbol and two dots, this pattern i am going to match with the test string test string is xylz, and i am performing match using match method.

if match is found am printing search operation is successful.

if match is not found i should print search operation unsuccessful what output we will get when we run this code yes, we will get search operation is successful so most of you might be thinking we get that answer because x and z are present in the string ofcourse x and z are present, but what are the mean ing of these symbols, to understand that lets move onto next topic.

meta characters with example.

here we will understand different symbols with their mean ing here i have a list of symbols and their mean ing we will take up with square bracket.

this specifies, set of character we wish to match, so what ever the characters you want to match you can specify inside this square bracket the dot symbol match any character except new line.

you can call it as period also to build regular expression and test i will be using regx101 testing tool lets understand how these symbols work , once you open this testing tool, here you can find regular expression, where you will be writing regex in this box and below we will be writing testing string.

and on the right hand side we have explanation and match information.

if any match is found that is displayed in match information lets start with square bracket first it is used to match range of values i will give a -d test string has axyz we have one match because in the string i have character a this bracket has characters from a-d, if the characters within this range are available in the string its matched. lets take another example abcxyz, so here you can see that we have matched character a and b and c instead of these characters you can specify numbers as well. period symbol, i have pattern as house and one dot, this period matches any character except new line charcter now i will write house but it is telling regx doent match the substring that mean s atleast one character should be there after this house.

now i will write s, we have got one match now.

though i have written two more ss it is matching only the 6 characters suppose instead of one period, if i give two period what happens , it is taking next s also inside that match, so how many periods you give thse many characters it is going to match.

$ symbl is used to check the string ends with certain charcter carot is used to check if it starts with certain character.

just look at this pattern in this pattern we are telling that our string should start at x and end with z, and two dots represents there should be two character in between x and z lets test carot and $symbol, i will check whether string starts with char h and ends with char o in between h and o there should be three characters first i will write hello, here you need to change regx flag to multiline, now you can see it is matching string is starting with h and ending with o, if i give helllo, whether it is matching? no, its not matching because i need to have only three characters i will start with o, end with h it wont match, whenever you want to match start and end of the character of the string you can use carot and $ now i will take [] bracket in combination with carot symbol i want match three numbers i will give 4 123, so what happened only 1 match , 4 what does this mean inside the square bracket if you give carot symbol it tells you that apart from these numbers any other number next we have star symbol asterisk it specifies 0 or more occurrences of the pattern left to it, you will understand it better when we take an an example, and then we have + symbol it matches one or more occurances ? question mark tells zer or one occurrence of the pattern first lets understand asterisk metacharacter pattern as girl , i will put * before l , what does it mean s, that mean s the charcter preecding this can occur 0 times or more times, girl, it gets one match instead of i will remove r, it is still matching because r becomes optional it may be present or not, now i will give irl it is not matching, because these are the compulsory characters now i will girrrl, yes it is matching because more occurrences of r gird so, you can see we are getting no match because r is not followed l instead it is d, thats why it is not matching i hope you are clear, now instead of * i will give + i will gil no match, because star matches 0 or more occurrences, wherea s + matches one or more occurance atleast one time the char r should be present now it is getting match for more than one times r , still i am getting the match instead of + i will give question mark, it match 0 or one occurrence of the match i will give gil i am getting match, becasue there is 0 occurrence girl if i give, it is match and next girrl, here it is not matching , because question mark matches only 0 or 1 occurance.

here r is coming twice thats why it is giving no match.

next we have flower brackets, matches exact the specified number of occurances vertical bar, this is alternation, this is like or operation.

and then we have parantheses used to group sub pattern lets understand flower brackets i will match character x it should be present in my string two times, i have abcx you can observe here, we are not getting match because, x is present only once so x should be present atleast two times, then only i wil get the match, what is i have one more x 1 match itslf, if i put one more x now we have two matches suppose you want to define maximum number of times your character should be present in the string here i can specify the number as well x should be present atleast 2 times, and atmost 4 times.

one time x , no match atleast two times, we are getting match, three times, still we are getting match x cam be present inbetween 2 and four times.

now lets discuss alternation, that is or symbol lets say i have it is equivalent to boolean or i matches , if in our string cat is present or dog is present it matches.

cat is my pet animal it is matching cat, instead of cat if i put dog here , yes still it is matching.

parantheses i have (x|y|z) abc this mean s that string should contain either x y z along with abc i give abc, no match, instead of abc if if add xabc, we are getting match if i add y still we are getting a match if we add d, no we are not getting match because only xy z are allowed along with abc we are grouping these characters with abc backslash operation it is escape sequence $, you cannot consider $ as special character here 100$, it is matching, because it is not considered as special character if i say 100.

we have to define . with backslash i hope now you have the basic idea of these special characters or meta characters you will find its more uses when we take up some use cases, and write code.


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method