希望找到一个reg ex表达式(关于芬德尔)每四个单词在另一个(多行)字符串上有两个元音的单词?

2024-04-25 17:59:46 发布

您现在位置:Python中文网/ 问答频道 /正文

 textDoc= 
 Line 1 "'"Here is my Text which 
 Line 2 I now have *starred* the words which i would like accounted for"
 Line 3 I would like the end result to be Lines 3 Words 6.
 Line 4.Python Regular expression **rules** have me trying things that 
 Line 5.I have listed below. All of them are usable but I would like 
 Line 6 To understand how to customize it for **production** to use."""

//desiredoutput = Lines 3, Words 3 
/* This is because the words: starred, rules, and production are on every 
   other line and they contain more than 2 vowels all while being the fourth 
   word on the line.*/

我似乎不能把它放在一起,但一些正则表达式代码,我正在考虑和工作了一点点,迄今为止是:

enumerate, .split. find.All 

[aeiou],[aeiou]{2},



 textDoc = 
numOfLines = len(textDoc.splitlines())
print(numOfLines)

split将单词列表转换为字符串。我想我需要一个新的字符串,每四个字在每一行之前,我数他们来完成我想要的输出行3个字3


Tags: thetowhichforishavelinerules
1条回答
网友
1楼 · 发布于 2024-04-25 17:59:46

我认为在python中不能使用一个regex,因为回溯,下面的解决方案可以使用perl regex,因为它使用控制动词(*SKIP)

(?:[a-z]+(?:(?!\n)[^a-z])+){3}((?=(?:(?![aeiou])[a-z])*[aeiou](?:(?![aeiou])[a-z])*[^a-z](*SKIP)(?!)|)[a-z]+)

regex101 link

使用python的最接近的方法由于回溯而不起作用

(?:[a-z]+(?:(?!\n)[^a-z])+){3}((?=(?:(?:(?![aeiou])[a-z])*[aeiou]){2})[a-z]+)

regex101 link

相关问题 更多 >