gpt4 book ai didi

python - 获取数据仅以空格开头 Regex - Python

转载 作者:行者123 更新时间:2023-12-04 07:39:43 25 4
gpt4 key购买 nike

我想让所有行都以空格开头
初始文本示例:

!
line con 0
session-timeout 5
exec-timeout 5 0
password 7 1239211A43054F0202D1D
transport output none
line 2
no activation-character
no exec
transport preferred none
transport output pad telnet rlogin lapb-ta mop udptn v120 ssh
stopbits 1
line vty 0 4
session-timeout 5
access-class 125 in
exec-timeout 5 0
length 0
transport input ssh
transport output none
结果必须是:
line con 0
session-timeout 5
exec-timeout 5 0
password 7 1239211A43054FB202D1D
transport output none
我在 尝试了一些东西 python
result = re.findall('line\scon(?s).*?(?=^\w)', str(abovetext))

最佳答案


(?m)^line\scon.*(?:\n .+)*
regex proof .
说明
--------------------------------------------------------------------------------
(?m) set flags for this block (with ^ and $
matching start and end of line) (case-
sensitive) (with . not matching \n)
(matching whitespace and # normally)
--------------------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------------------
line 'line'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
con 'con'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
.+ any character except \n (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)* end of grouping
Python代码 :
result = re.findall(r'^line\scon.*(?:\n .+)*', str(abovetext), re.M)

关于python - 获取数据仅以空格开头 Regex - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67550758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com