123
Python 3 正規表現モジュールを使用して次の文字列の一部を取得するにはどうすればよいですか?
....XX (a lot of HTML characters)123
ここで、...
Part は、HTML 文字、単語、および数字で構成される長い文字列を表します。
数123
が特徴ですXX
。したがって、またはのXX
ような任意の文字を使用できる普遍的な方法を誰かが提案できれば、より役に立ちます。AA
AB
補足:
Perl の\G
演算子を使用して、最初XX
に文字列を特定し、次に の後に現れる最初の数字を特定することを考えましたXX
。しかし、\G
演算子は Python 3 では機能しないようです。
私のコード:
import re
source='abcd XX blah blah 123 more blah blah'
grade=str(input('Which grade?'))
#here the user inputs XX
match=re.search(grade,source)
match=re.search('\G\D+',source)
#Trying to use the \G operator to get the location of last match.Doesn't work.
match=re.search('\G\d+',source)
#Trying to get the next number after XX.
print(match.group())