
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why do we use question mark literal in Python regular expression?
The question mark literal is used in different ways in Python Regular Expressions
Special character '?'
As a special character '?' causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’
The question mark literal '?' is used following ways as per Python docs.
Making the quantifier non-greedy
*?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against '<a> b <c>', it will match the entire string, and not just '<a>'. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only '<a>'.
- Related Questions & Answers
- Why do we use re.compile() method in Python regular expression?
- How do we use Python Regular Expression named groups?
- How do we use re.finditer() method in Python regular expression?
- Why we should use whole string in Java regular expression
- How do we use Python regular expression to match a date string?
- How do we use a delimiter to split string in Python regular expression?
- Why do we use random.seed() in Python?
- Why do we use pandas in python?
- Why do we use interfaces in Java?
- How to use wildcard in Python regular expression?
- How to use range in Python regular expression?
- How to use variables in Python regular expression?
- Why do we use jQuery over JavaScript?
- How do we use function literal to define a function in JavaScript?
- Why do we use JSON.stringify() method in jQuery?
Advertisements