
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to 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, utilise the re package.
To match with the beginning of the string in python by using regular expression, we use ^/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 occurrence of a character.
Using re.search() method
In the following example code, we match the word tutorialspoint which is present at the beginning of the string ‘tutorialspoint is a great platform to enhance your skills’.
We begin by importing regular expression module.
import re
Then, we have used search() function which is imported from the re module to get the required string. This 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
import re s = 'tutorialspoint is a great platform to enhance your skills' result = re.search(r'^\w+', s) print (result.group())
Output
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 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())
Output
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
import re text = 'tutorialspoint is a great platform to enhance your skills in tutorialspoint' result = re.findall(r'^\w+', text) print(result)
Output
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)
Output
The first letter of the given string is: ['P']
- Related Articles
- How to match at the end of string in python using Regular Expression?\n\n
- How to match only digits in Python using Regular Expression?
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to match parentheses in Python regular expression?
- How do we use Python regular expression to match a date string?
- How to match any one lowercase vowel in python using Regular Expression?
- How to match a non-whitespace character in Python using Regular Expression?
- How to match a regular expression against a string?
- Program to match vowels in a string using regular expression in Java
- How to match digits using Java Regular Expression (RegEx)
- How to match nonword characters in Python using Regular Expression?\n\n\n\n
- How to match any one uppercase character in python using Regular Expression?\n\n
- How to match any non-digit character in Python using Regular Expression?\n\n
