How to check if a given string matches a particular pattern in Python?

0 votes
I want to check whether an input string matches a particular pattern of characters. How to do it in Python?
Jul 3 in Python by Arvind
• 2,960 points
32 views

1 answer to this question.

0 votes

You can use re module of python for matching a specific pattern. re stands for regular expressions. For example

[A-Z] will match exactly one character between A and Z

\d+ will match one or more digits

() group things (and also return things... but for now just think of them grouping)

+ selects 1 or more

You can refer to the following code to understand it better

import re
pattern = re.compile("^([A-Z][0-9]+)+$")
pattern.match(string)
answered Jul 3 by Neel
• 3,010 points

Related Questions In Python

+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 56,520 points
2,689 views
0 votes
1 answer

How to check if a string ends with a character in python?

You can use the endswith method. It checks ...READ MORE

answered Apr 4 in Python by Srisha
55 views
0 votes
1 answer

How to check if a substring is present in a string using Python?

To check if the substring exists in ...READ MORE

answered May 9 in Python by Sharan
42 views
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 4 in Python by Shabnam
• 920 points
39 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 222 views
+4 votes
6 answers
0 votes
1 answer

how to check whether a string starts with a particular word?

For this, you can use the built-in ...READ MORE

answered Jul 17 in Python by Neel
• 3,010 points
22 views
0 votes
1 answer

How to print a string with curly braces in Python?

You can use something like this - mystring ...READ MORE

answered Jul 23 in Python by Neel
• 3,010 points
37 views