Search for strings in Python

How to search for a string in Python to determine if it contains any string and get its position. You can use the re module of the standard library for more flexible processing with regular expressions.

Judgment whether it contains an arbitrary character string

Use the in operator. Returns True if included, False if not included.

text = "I like TWICE"

print("TWICE" in text)
#True

print("twice" in text)
#False

print('TWICE' in text and 'I' in text)
# True

Get the position of any string

Get the position of any string in the string with find ()

If the string specified in the first argument is included in the caller's string, the position of the first character is returned, otherwise **-1 ** is returned.

text = "I like TWICE"

print(s.find('TWICE'))
# 7

print(s.find('XXX'))
# -1


Judgment by regular expression, position acquisition

Use re.search () to determine if a regular expression contains a particular string.

Specify the regular expression pattern character string in the first argument and the target character string in the second argument. You can use metacharacters for the regular expression pattern, but use the search string as it is.

A match object is returned if there is a match, and None is returned if there is no match.

import re

text = 'I like TWICE'

print(re.search('TWICE', text))
# <re.Match object; span=(7, 12), match='TWICE'>

print(re.search('XXX', s))
# None

Since the match object is always judged as True, if you want to make a conditional branch with an if statement, you can specifyre.search ()or its return value as it is as a conditional expression.


Match object method group () returns the matched string, start (), end (), span () returns the tuple of start position, end position, (start position, end position) respectively.

m = re.search('TWICE', text)

print(m.group())
# TWICE

print(m.start())
# 7

print(m.end())
# 12

print(m.span())
# (5, 8)

Get all results with regular expression

re.findall () returns all matching parts as a list of strings. The number of parts where the number of elements (obtained by the built-in function len ()) matches

text = "I am dream" #Ignore the meaning of the word

print(re.findall('am', text))
# ['am', 'am']

print(len(re.findall('am', text)))
# 2

If you want to get the position of all parts, combine re.finditer () with list comprehension notation

print([m.span() for m in re.finditer('am', text)])
# [(2, 4), (8, 10)]

Search for multiple strings with regular expressions

Regular expression patternA|BThenAOrBMatch to.A, BCan be just a string. Same for 3 or moreA|B|CAnd it is sufficient.

text = 'I am Sam Adams'

print(re.findall('Sam|Adams', text))
# ['Sam', 'Adams']

print([m.span() for m in re.finditer('Sam|Adams', text)])
# [(5, 8), (9, 14)]

Take advantage of regular expression patterns

More flexible search with regular expression metacharacters and special sequences

text = 'I am Sam Adams'

print(re.findall('am', text))
# ['am', 'am', 'am']

print(re.findall('[a-zA-Z]+am[a-z]*', text))
# ['Sam', 'Adams']

Recommended Posts

Search for strings in Python
Search for strings in files
Binary search in Python
Linear search in Python
Compare strings in Python
Reverse strings in Python
Binary search in Python (binary search)
Techniques for sorting in Python
Binary search in Python / C ++
Algorithm in Python (binary search)
About "for _ in range ():" in python
Recursively search for files and directories in Python and output
Check for memory leaks in Python
Algorithm in Python (breadth-first search, bfs)
Write a binary search in Python
Check for external commands in python
Algorithm in Python (depth-first search, dfs)
Write a depth-first search in Python
Run unittests in Python (for beginners)
Depth-first search using stack in Python
Extract strings from files in Python
Google search for the last line of the file in Python
2016-10-30 else for Python3> for:
Quadtree in Python --2
Python in optimization
python [for myself]
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Algorithm in Python (ABC 146 C Binary Search
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Template for writing batch scripts in python
Programming in python
Process multiple lists with for in Python
Plink in Python
Constant in python
MongoDB for the first time in Python
Get a token for conoha in python
Lifegame in Python.
FizzBuzz in Python
Sample for handling eml files in Python
Bulk replacement of strings in Python arrays
Sqlite in python
StepAIC in Python
Search for character strings in files [Comparison between Bash and PowerShell]
AtCoder cheat sheet in python (for myself)
N-gram in python