如何在一个正则表达式中捕获所有正则表达式组?
How to capture all regex groups in one regex?
给定一个这样的文件:
# For more information about CC-CEDICT see:
# http://cc-cedict.org/wiki/
A A [A] /(slang) (Tw) to steal/
AA制 AA制 [A A zhi4] /to split the bill/to go Dutch/
AB制 AB制 [A B zhi4] /to split the bill (where the male counterpart foots the larger portion of the sum)/(theater) a system where two actors take turns in acting the main role, with one actor replacing the other if either is unavailable/
A咖 A咖 [A ka1] /class "A"/top grade/
A圈兒 A圈儿 [A quan1 r5] /at symbol, @/
A片 A片 [A pian4] /adult movie/pornography/
我想构建一个 json 对象:
- 跳过以
#
开头的行
- 将行分成 4 部分
- 传统字符(从开始
^
到下一个 space)
- 简化字符(从第一个space到第二个)
- 拼音(跨越方括号
[...]
)
- 第一个
/
到最后一个 /
之间的斜线 space (请注意,在某些情况下,斜线内可能有斜线,例如 /adult movie/pornography/
我目前是这样做的:
>>> for line in text.split('\n'):
... if line.startswith('#'): continue;
... line = line.strip()
... simple, _, line = line.partition(' ')
... trad, _, line = line.partition(' ')
... print simple, trad
...
A A
AA制 AA制
AB制 AB制
A咖 A咖
A圈兒 A圈儿
A片 A片
要获得 [...]
,我必须这样做:
>>> import re
>>> line = "A片 A片 [A pian4] /adult movie/pornography/"
>>> simple, _, line = line.partition(' ')
>>> trad, _, line = line.partition(' ')
>>> re.findall(r'\[.*\]', line)[0].strip('[]')
'A pian4'
要找到 /.../
,我必须这样做:
>>> line = "A片 A片 [A pian4] /adult movie/pornography/"
>>> re.findall(r'\/.*\/$', line)[0].strip('/')
'adult movie/pornography'
我如何使用正则表达式组一次捕获所有这些,同时执行多个 partitions/splits/findall?
这可能有帮助:
preg = re.compile(r'^(?<!#)(\w+)\s(\w+)\s(\[.*?\])\s/(.+)/$',
re.MULTILINE | re.UNICODE)
with open('your_file') as f:
for line in f:
match = preg.match(line)
if match:
print(match.groups())
查看 here 以了解所用正则表达式的详细说明。
p = re.compile(ru"(\S+)\s+(\S+)\s+\[([^\]]*)\]\s+/(.*)/$")
m = p.match(line)
if m:
simple, trad, pinyin, gloss = m.groups()
有关详细信息,请参阅 https://docs.python.org/2/howto/regex.html#grouping。
我创建了以下正则表达式来匹配所有四个组:
^(.*)\s(.*)\s(\[.*\])\s(\/.*\/)
这确实假定组之间只有一个 space,但是如果您有更多组,您可以添加一个修饰符。
这是一个演示,它如何与 python 一起使用问题中提供的行:
我可以改用正则表达式来提取信息。这样,您可以分组捕获块,然后根据需要处理它们:
import re
with open("myfile") as f:
data = f.read().split('\n')
for line in data:
if line.startswith('#'): continue
m = re.search(r"^([^ ]*) ([^ ]*) \[([^]]*)\] \/(.*)\/$", line)
if m:
print(m.groups())
即正则表达式将字符串分成以下几组:
^([^ ]*) ([^ ]*) \[([^]]*)\] \/(.*)\/$
^^^^^ ^^^^^ ^^^^^ ^^
1) 2) 3) 4)
即:
第一个字
第二个字
[
和]
内的文字。
从 /
到行尾前 /
的文本。
它returns:
('A', 'A', 'A', '(slang) (Tw) to steal')
('AA制', 'AA制', 'A A zhi4', 'to split the bill/to go Dutch')
('AB制', 'AB制', 'A B zhi4', 'to split the bill (where the male counterpart foots the larger portion of the sum)/(theater) a system where two actors take turns in acting the main role, with one actor replacing the other if either is unavailable')
('A咖', 'A咖', 'A ka1', 'class "A"/top grade')
('A圈兒', 'A圈儿', 'A quan1 r5', 'at symbol, @')
('A片', 'A片', 'A pian4', 'adult movie/pornography')
给定一个这样的文件:
# For more information about CC-CEDICT see:
# http://cc-cedict.org/wiki/
A A [A] /(slang) (Tw) to steal/
AA制 AA制 [A A zhi4] /to split the bill/to go Dutch/
AB制 AB制 [A B zhi4] /to split the bill (where the male counterpart foots the larger portion of the sum)/(theater) a system where two actors take turns in acting the main role, with one actor replacing the other if either is unavailable/
A咖 A咖 [A ka1] /class "A"/top grade/
A圈兒 A圈儿 [A quan1 r5] /at symbol, @/
A片 A片 [A pian4] /adult movie/pornography/
我想构建一个 json 对象:
- 跳过以
#
开头的行
- 将行分成 4 部分
- 传统字符(从开始
^
到下一个 space) - 简化字符(从第一个space到第二个)
- 拼音(跨越方括号
[...]
) - 第一个
/
到最后一个/
之间的斜线 space (请注意,在某些情况下,斜线内可能有斜线,例如/adult movie/pornography/
- 传统字符(从开始
我目前是这样做的:
>>> for line in text.split('\n'):
... if line.startswith('#'): continue;
... line = line.strip()
... simple, _, line = line.partition(' ')
... trad, _, line = line.partition(' ')
... print simple, trad
...
A A
AA制 AA制
AB制 AB制
A咖 A咖
A圈兒 A圈儿
A片 A片
要获得 [...]
,我必须这样做:
>>> import re
>>> line = "A片 A片 [A pian4] /adult movie/pornography/"
>>> simple, _, line = line.partition(' ')
>>> trad, _, line = line.partition(' ')
>>> re.findall(r'\[.*\]', line)[0].strip('[]')
'A pian4'
要找到 /.../
,我必须这样做:
>>> line = "A片 A片 [A pian4] /adult movie/pornography/"
>>> re.findall(r'\/.*\/$', line)[0].strip('/')
'adult movie/pornography'
我如何使用正则表达式组一次捕获所有这些,同时执行多个 partitions/splits/findall?
这可能有帮助:
preg = re.compile(r'^(?<!#)(\w+)\s(\w+)\s(\[.*?\])\s/(.+)/$',
re.MULTILINE | re.UNICODE)
with open('your_file') as f:
for line in f:
match = preg.match(line)
if match:
print(match.groups())
查看 here 以了解所用正则表达式的详细说明。
p = re.compile(ru"(\S+)\s+(\S+)\s+\[([^\]]*)\]\s+/(.*)/$")
m = p.match(line)
if m:
simple, trad, pinyin, gloss = m.groups()
有关详细信息,请参阅 https://docs.python.org/2/howto/regex.html#grouping。
我创建了以下正则表达式来匹配所有四个组:
^(.*)\s(.*)\s(\[.*\])\s(\/.*\/)
这确实假定组之间只有一个 space,但是如果您有更多组,您可以添加一个修饰符。
这是一个演示,它如何与 python 一起使用问题中提供的行:
我可以改用正则表达式来提取信息。这样,您可以分组捕获块,然后根据需要处理它们:
import re
with open("myfile") as f:
data = f.read().split('\n')
for line in data:
if line.startswith('#'): continue
m = re.search(r"^([^ ]*) ([^ ]*) \[([^]]*)\] \/(.*)\/$", line)
if m:
print(m.groups())
即正则表达式将字符串分成以下几组:
^([^ ]*) ([^ ]*) \[([^]]*)\] \/(.*)\/$
^^^^^ ^^^^^ ^^^^^ ^^
1) 2) 3) 4)
即:
第一个字
第二个字
[
和]
内的文字。从
/
到行尾前/
的文本。
它returns:
('A', 'A', 'A', '(slang) (Tw) to steal')
('AA制', 'AA制', 'A A zhi4', 'to split the bill/to go Dutch')
('AB制', 'AB制', 'A B zhi4', 'to split the bill (where the male counterpart foots the larger portion of the sum)/(theater) a system where two actors take turns in acting the main role, with one actor replacing the other if either is unavailable')
('A咖', 'A咖', 'A ka1', 'class "A"/top grade')
('A圈兒', 'A圈儿', 'A quan1 r5', 'at symbol, @')
('A片', 'A片', 'A pian4', 'adult movie/pornography')