简体   繁体   English

当关键字出现在模式之后时,python 拆分字符串

[英]python split a string when a keyword comes after a pattern

I have a hostname like我有一个主机名

ab-test-db-dev.0002-colo1-vm234.abc.domain.com

(yeah there is no convention followed internally for hostname.) (是的,主机名内部没有遵循任何约定。)

I was trying to split this hostname to我试图将此主机名拆分为

ab-test-db-dev.0002-colo1-vm234

pattern is to split with the '.', but only if there are no other special characters following that dot.模式是用 '.' 分割,但前提是该点后面没有其他特殊字符。

I tried我试过

pattern = domain.split(".")

but it is taking only till但它只需要直到

ab-test-db-dev and not ab-test-db-dev.0002-colo1-vm234

as the first element.作为第一个元素。

What is the best way to achieve this?实现这一目标的最佳方法是什么?

You could remove the first part until no more dashes remain;您可以删除第一部分,直到不再有破折号; that'd be the domain name to remove from the hostname:这将是要从主机名中删除的域名:

hostname = domain
while '-' in domain:
    domain = domain.partition('.')[-1]
hostname = hostname[:-len(domain) - 1]

or the other way around, remove the last part if it doesn't contain dashes, with str.rpartition() :或者str.rpartition() ,如果最后一部分包含破折号,则使用str.rpartition()删除它:

hostname = domain
while True:
    first, _, end = hostname.rpartition('.')
    if '-' in end:
        break
    hostname = first

Using a regular expression looking for any part that only contains letters and dots:使用正则表达式查找仅包含字母和点的任何部分:

import re

hostname = re.sub(r'\.[a-z.]+$', '', domain)

Demo:演示:

>>> domain = 'ab-test-db-dev.0002-colo1-vm234.abc.domain.com'
>>> hostname = domain
>>> while '-' in domain:
...     domain = domain.partition('.')[-1]
... 
>>> hostname[:-len(domain) - 1]
'ab-test-db-dev.0002-colo1-vm234'
>>> domain = 'ab-test-db-dev.0002-colo1-vm234.abc.domain.com'
>>> hostname = domain
>>> while True:
...     first, _, end = hostname.rpartition('.')
...     if '-' in end:
...         break
...     hostname = first
... 
>>> hostname
'ab-test-db-dev.0002-colo1-vm234'
>>> import re
>>> re.sub(r'\.[a-z.]+$', '', domain)
'ab-test-db-dev.0002-colo1-vm234'

Didn't get the pattern but for this case the following can work.没有得到模式,但对于这种情况,以下可以工作。

(?<=\d)\.

Try this:尝试这个:

https://regex101.com/r/rU8yP6/21 https://regex101.com/r/rU8yP6/21

Use re.split .使用re.split

 import re
 re.split(r"(?<=\d)\.",test_Str)

Or或者

^(.*?)(?!.*-)\.

Try this:尝试这个:

https://regex101.com/r/rU8yP6/22 https://regex101.com/r/rU8yP6/22

import re
print re.findall(r"^(.*?)(?!.*-)\.",test_str)

If I understood your question correctly, then this regex should do the job:如果我正确理解你的问题,那么这个正则表达式应该可以完成这项工作:

.*?(?=\\.(?!.*[^\\w.]))

>>> print re.match(r'.*?(?=\.(?!.*[^\w.]))', 'ab-test-db-dev.0002-colo1-vm234.abc.domain.com')
ab-test-db-dev.0002-colo1-vm234

Explanation:解释:

.*? # match everything up to...
(?=
    \. # the first dot...
    (?! # that isn't followed by...
        .* # any text and...
        [^\w.] # something that's not a word character or a dot.
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM