

This will extract all of the numbers characters.We are using ‘\w’ to represent any letter, numeric digit or the underscore character.Įxample 3: Extract only numeric digits from it import re.This will extract all of the word characters i.e.It is happening because dot(.) represents character and space is also a character.Įxample 2: Change above code so that it extracts only word character import re It is also finding space and adding it in output.Using search() will just gives us the first match. We are using findall() as we need to extract all of the matches.We are first defining the pattern than converting it into a regex object and searching for this pattern in the stored paragraph/text.I have constructed the code so that its flow is consistent with the flow mentioned in the last tutorial blog post on Python Regular Expression.Raw string makes it easier to create pattern as it doesn’t escapes any character. One thing you should notice is that pattern is saved as a raw string (r’.’).To use python regular expression, we need re package.Above code will extract all of the characters and display it.With its eighth season premiering in 2019.''' Example 1 : Extract all characters from the paragraph using Python Regular Expression. Para='''Game of Thrones is an fantasy drama television seriesĬreated by David Benioff and D. It will be easier to call it in any function. With its eighth season premiering in 2019.'įirst of all, lets save it in a variable named as ‘para’. Premiered on HBO in the United States on April 17, 2011, and its Iceland, Malta, Morocco, Spain, and the United States. It is filmed inīelfast and elsewhere in the United Kingdom, Canada, Croatia, Novels, the first of which is A Game of Thrones. It is an adaptation ofĪ Song of Ice and Fire, George R. You can also copy it from below:- 'Game of Thrones is an American fantasy drama television seriesĬreated by David Benioff and D. Now, let’s copy the first paragraph from it. I have chosen a Wikipedia article (on Game of Thrones). We will need a text set using which we can practice and get results. We will work out some examples and can try to understand how to use python regular expression to solve our problem. If you know, then let’s practice some of the concept mentioned. If you don’t know the basic syntax and structure of it, then it will be better to read the mentioned post.


In the last post ( Beginner’s Guide to Python Regular Expression), we learnt about python regular expression.
