How do I return the index value of a regularly matched object in python?
The expression in the
question is a little unclear: specifically, the reason is a malfunction of the CPU circuit board and needs to be replaced.
B="CPU circuit board"
import re
C = re.search (A Magi B)
print (C)
how to get the index value of CPU circuit board in A.
/ / ps. The datasets used to build named entities, notice that they all seem to be built in the following form, unknown and without misunderstanding.
original O
because O
is O
C B-PART
P I-PART
U I-PART
electric I-PART
I-PART
board I-PART
so O
barrier O
, O
need O
O
enter O
go O
change O
change O
. O
A= 'CPU'
B='CPU'
import re
C = re.search(B, A)
print(':', C.span()[0], '-', C.span()[1])
print(':', A.find(B), '-',A.find(B) + len(B))
print(':', A.index(B), '-',A.index(B) + len(B))
there are three ways to show you.
correct the error.
more refined content
< hr >
A= 'CPUCPU'
B='CPU'
import re
print((['{} - {}'.format(m.start(), m.start()+len(B)) for m in re.finditer(B, A)]))
print([(m.start(), m.start()+len(B)) for m in re.finditer(B, A)])
continue to go further
< hr >
>>> A= 'CPUCPU'
>>> B='|CPU|'
>>> import re
>>> print([(m.group(), m.span()) for m in re.finditer(B, A)])
[('CPU', (3, 9)), ('', (14, 18)), ('CPU', (23, 29))]
C = re.search(B, A)
print(C.start())
if you don't need rules, you can use find directly
A.find(B)