Use re.search
>>> import re
>>> s="Part 1. Part 2. Part 3 then more text"
>>> re.search(r'Part 1\.(.*?)Part 3', s).group(1)
' Part 2. '
>>> re.search(r'Part 1(.*?)Part 3', s).group(1)
'. Part 2. '
Or use re.findall
, if there are more than one occurances.
More Related Contents:
- Empty string instead of unmatched group error
- What exactly do “u” and “r” string flags do, and what are raw string literals?
- How can I force division to be floating point? Division keeps rounding down to 0?
- Why does the division get rounded to an integer?
- How can I read large text files line by line, without loading it into memory?
- Match a whole word in a string using dynamic regex
- How to check for valid email address? [duplicate]
- Case insensitive regular expression without re.compile?
- Easiest way to replace a string using a dictionary of replacements?
- Recursive pattern in regex
- Find longest repetitive sequence in a string
- Python Regex instantly replace groups
- How to remove parentheses and all data within using Pandas/Python?
- Regular Expressions: Search in list
- Return string with first match for a regex, handling case where there is no match
- What’s the cleanest way to extract URLs from a string using Python?
- Python 2 CSV writer produces wrong line terminator on Windows
- Printing without newline (print ‘a’,) prints a space, how to remove?
- Regular expression: match start or whitespace
- Remove duplicate chars using regex?
- What is more ‘pythonic’ for ‘not’ [duplicate]
- Python replace string pattern with output of function
- How can I make multiple replacements in a string using a dictionary?
- Printing a string prints ‘u’ before the string in Python?
- Creating a custom categorized corpus in NLTK and Python
- How do implicit relative imports work in Python?
- Python regex findall
- Regexp finding longest common prefix of two strings
- Splitting a math expression string into tokens in Python
- Download all pdf files from a website using Python