Match at the Beginning of String in Python Using Regular Expression



A regular expression in Python is a group of characters that allows you to use a search pattern to find a string or set of strings. RegEx is a term for regular expressions. To work with regular expressions in Python, use the re package.

Understanding String Matching in Python

String matching is a basic Python method that allows you to look for and find patterns in a given text. The match() function, a component of the re (regular expression) package, is one of the widely used methods for this purpose.

Use the match() method to determine whether the beginning of a string matches a specific pattern. It returns Null if the pattern is not at the beginning of the string, and returns a match object otherwise.

Before using the match() function, you will have to create a regular expression pattern that reflects the desired string format. Regular expressions are a better and flexible way for describing and matching text patterns.

Key Symbols in Regular Expressions

To match the beginning of the string in Python by using a regular expression, we use the ^/w+ regular expression.

Here,

  • ^ implies the start with.
  • /w returns a match where the string contains any word characters (a-z, A-Z, 0-9, and underscore character).
  • + indicates one or more occurrences of a character.

Using re.search() method

The re.search() function in Python searches the string for a match and returns a match object if there is any match. The group() method is used to return the part of the string that is matched.

Example

Following is an example -

import re
s = 'tutorialspoint is a great platform to enhance your skills'
result = re.search(r'^\w+', s)
print (result.group())

The following output is obtained on executing the above program.

tutorialspoint

Example 2

Now, let us find out the first letter of a single string using the re.search() method in Python -

import re
s = 'Program'
result = re.search(r"\b[a-zA-Z]", s)
print ('The first letter of the given string is:',result.group())

The following is the output of the above program -

The first letter of the given string is: P

Using re.findall() method

The method findall(pattern, string) in Python locates every occurrence of the pattern within the string. The caret (^) guarantees that you only match the word Python at the beginning of the string when you use the pattern "^\w+".

Example

Following is an example using the findall() method -

import re
text = 'tutorialspoint is a great platform to enhance your skills in tutorialspoint'
result = re.findall(r'^\w+', text)
print(result)

The substring 'tutorialspoint' appears twice, but there is only one place in the string where it matches, which is at the beginning, as seen in the output below -

['tutorialspoint']

Example

Now, let us find out the first letter of a single string using re.findall() method in Python -

import re
s = 'Program'
result = re.findall(r"\b[a-zA-Z]", s)
print ('The first letter of the given string is:',result)

The following is the output of the above program -

The first letter of the given string is: ['P']

Validating an Email Address

Regular expressions are widely used in many different applications, including data validation and text processing.

import re
email = "test@example.com"
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
match = re.match(pattern, email)
print("Valid Email" if match else "Invalid Email")

The following is the output of the above program -

Valid Email
Updated on: 2025-04-23T17:38:20+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements