替换字符串python中的特定字符串模式
我有一些带有表情符号Unicode的句子,它们由Unicode模式组成,如U0001。我需要将所有具有U0001的字符串提取到一个数组中。这是我尝试过的代码替换字符串python中的特定字符串模式,python,regex,unicode,Python,Regex,Unicode,我有一些带有表情符号Unicode的句子,它们由Unicode模式组成,如U0001。我需要将所有具有U0001的字符串提取到一个数组中。这是我尝试过的代码 import re pattern = re.compile(r"^U0001") sentence = 'U0001f308 U0001f64b The dark clouds disperse the hail subsides and one neon lit rainbow wi
import re
pattern = re.compile(r"^U0001")
sentence = 'U0001f308 U0001f64b The dark clouds disperse the hail subsides and one neon lit rainbow with a faint second arches across the length of the A u2026'
print(pattern.match(sentence).group()) #this prints U0001 every time but what i want is ['U0001f308']
matches = re.findall(r"^\w+", sentence)
print(matches) # This only prints the first match which is 'U0001f308'
有没有办法将字符串提取到数组中?。我在正则表达式方面没有太多经验。
'U0001f30'
不是表情符号代码点!它是一个9个字符的字符串,以字母“U”开头
输入超过4个十六进制字符的unicode密码点的方法是\U0001f308
。同样,要输入4个十六进制字符的代码点:\u0001
但不能像查找常规字符串一样查找以“0001”开头的代码点。在我看来,您可能正在查找4个十六进制字符的代码点\u0001
或范围\U00010000-\U0001FFFF
中的任何内容:
import re
sentence = '\U0001f308 \U0001f64b The dark clouds disperse the hail subsides and one neon lit rainbow with a faint second arches across the length of the A \u2026'
matches = re.findall('[\u0001\U00010000-\U0001FFFF]', sentence)
print(matches)
matches -> ['\U0001f308', '\U0001f64b']
如果出于某种原因,您确实有以“U”开头的字符串,而不是实际的代码点,那么:
matches = re.findall('U0001(?:[0-9a-fA-F]{4})?', sentence)
我还假设emojis可以位于字符串中的任何位置,并且可以与任何其他字符相邻。您能试试这个吗?(\bU0001\w*)\b
^
只在字符串的开头搜索