首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >字符串的Regex表达式

字符串的Regex表达式
EN

Stack Overflow用户
提问于 2019-11-07 07:04:45
回答 3查看 157关注 0票数 1

我想在python中分割字符串。

样本字符串:

嗨,我是ACT I,第1和第2幕,这是ACT II,第1和第2幕及更多。

列入下列清单:

代码语言:javascript
运行
复制
['Hi this is', 'ACT I. SCENE 1', 'and', 'SCENE2', 'and this is', 'ACT II. SCENE 1',
 'and' , 'SCENE 2', 'and more']

有人能帮我建战壕吗?我建造的是:

代码语言:javascript
运行
复制
(ACT [A-Z]+.\sSCENE\s[0-9]+)]?(.*)(SCENE [0-9]+)

但这是不正常的。

EN

回答 3

Stack Overflow用户

发布于 2019-11-07 07:21:39

如果我正确理解您的需求,您可以使用以下模式:

代码语言:javascript
运行
复制
(?:ACT|SCENE).+?\d+|\S.*?(?=\s?(?:ACT|SCENE|$))

演示

分解:

代码语言:javascript
运行
复制
(?:                    # Start of a non-capturing group.
    ACT|SCENE          # Matches either 'ACT' or 'SCENE'.
)                      # Close the non-capturing group.
.+?                    # Matches one or more characters (lazy matching).
\d+                    # Matches one or more digits.
|                      # Alternation (OR).
\S                     # Matches a non-whitespace character (to trim spaces).
.*?                    # Matches zero or more characters (lazy matching).
(?=                    # Start of a positive Lookahead (i.e., followed by...).
    \s?                # An optional whitespace character (to trim spaces).
    (?:ACT|SCENE|$)    # Followed by either 'ACT' or 'SCENE' or the end of the string.
)                      # Close the Lookahead.

Python示例:

代码语言:javascript
运行
复制
import re

regex = r"(?:ACT|SCENE).+?\d+|\S.*?(?=\s?(?:ACT|SCENE|$))"
test_str = "Hi this is ACT I. SCENE 1 and SCENE 2 and this is ACT II. SCENE 1 and SCENE 2 and more"

list = re.findall(regex, test_str)
print(list)

输出:

代码语言:javascript
运行
复制
['Hi this is', 'ACT I. SCENE 1', 'and', 'SCENE 2', 'and this is', 'ACT II. SCENE 1', 'and', 'SCENE 2', 'and more']

在网上试试

票数 2
EN

Stack Overflow用户

发布于 2019-11-07 07:14:47

下面是一个工作脚本,尽管有点麻烦:

代码语言:javascript
运行
复制
inp = "Hi this is ACT I. SCENE 1 and SCENE 2 and this is ACT II. SCENE 1 and SCENE 2 and more"
parts = re.findall(r'[A-Z]{2,}(?: [A-Z0-9.]+)*|(?![A-Z]{2})\w+(?: (?![A-Z]{2})\w+)*', inp)
print(parts)

这些指纹:

代码语言:javascript
运行
复制
['Hi this is', 'ACT I. SCENE 1', 'and', 'SCENE 2', 'and this is', 'ACT II. SCENE 1',
 'and', 'SCENE 2', 'and more']

对regex逻辑的解释,该逻辑使用替换来匹配以下两种情况中的一种:

代码语言:javascript
运行
复制
[A-Z]{2,}              match TWO or more capital letters
(?: [A-Z0-9.]+)*       followed by zero or more words, consisting only of
                       capital letters, numbers, or period
|                      OR
(?![A-Z]{2})\w+        match a word which does NOT start with two capital letters
(?: (?![A-Z]{2})\w+)*  then match zero or more similar terms
票数 1
EN

Stack Overflow用户

发布于 2019-11-07 13:24:42

您可以使用re.findall

代码语言:javascript
运行
复制
import re
s = 'Hi this is ACT I. SCENE 1 and SCENE 2 and this is ACT II. SCENE 1 and SCENE 2 and more'
new_s = list(map(str.strip, re.findall('[A-Z\d\s\.]{2,}|^[A-Z]{1}[a-z\s]+|[a-z\s]+', s)))

输出:

代码语言:javascript
运行
复制
['Hi this is', 'ACT I. SCENE 1', 'and', 'SCENE 2', 'and this is', 'ACT II. SCENE 1', 'and', 'SCENE 2', 'and more']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58743530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档