Python Regex Lookahead and Lookbehind — A Helpful Guide

Python Lookahead You might find yourself working with regular expressions in Python, and the lookahead feature is quite handy in many situations. Lookahead comes in two flavors: positive lookahead and negative lookahead. Let’s explore these types of lookahead and see how you can use them in your Python code! ๐Ÿ Positive lookahead is an assertion … Read more

Python Regex Capturing Groups – A Helpful Guide (+Video)

Python’s regex capturing groups allow you to extract parts of a string that match a pattern. For example: match ='(\d+)’, ‘abc123’) captures the digits, and returns ‘123’. One of the powerful aspects of Python’s regular expression capabilities is the use of capturing groups. By using capturing groups, you can easily extract specific portions … Read more

How To Extract Numbers From A String In Python?

The easiest way to extract numbers from a Python string s is to use the expression re.findall(‘\d+’, s). For example, re.findall(‘\d+’, ‘hi 100 alice 18 old 42’) yields the list of strings [‘100′, ’18’, ’42’] that you can then convert to numbers using int() or float(). There are some tricks and alternatives, so keep reading … Read more

How to Count the Number of Words in a String in Python

Problem Formulation Given a string – sentence. How many words does the string sentence have within it? Examples: INPUT sentence = “Finxter helps you to master Python.” OUTPUT Number of words: 6 INPUT sentence = “””Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.””” OUTPUT … Read more

Python | Split Text into Sentences

โœจSummary: There are four different ways to split a text into sentences:๐Ÿš€ Using nltk module๐Ÿš€ Using re.split()๐Ÿš€ Using re.findall() ๐Ÿš€ Using replace Minimal Example Problem Formulation Problem: Given a string/text containing numerous sentences; How will you split the string into sentences? Example: Letโ€™s visualize the problem with the help of an example. Method 1: Using … Read more

Python | Split String with Regex

Summary: The different methods to split a string using regex are: Minimal Example Problem Formulation ๐Ÿ“œProblem: Given a string and a delimiter. How will you split the string using the given delimiter using different functions from the regular expressions library? Example: In the following example, the given string has to be split using a hyphen … Read more

Python | Split String Parenthesis

๐ŸŽSummary: You can split a string at parenthesis using re.split(r'[()]’, text) in a list comprehension accordingly. Minimal Example Problem Formulation ๐Ÿ“œProblem: Given a string. How will you split the string at parenthesis and spaces? Example In the above problem, you have been given a string separated by spaces and there are certain strings that are … Read more