从字符串中获取单词

时间:2019-04-25 16:27:51

标签: python regex

如何从这样的字符串中获取单词示例

str = "http://test-example:123/wd/hub"

我写这样的东西

print(str[10:str.rfind(':')])

但是它不能正常工作,如果字符串类似于

"http://tests-example:123/wd/hub" 

7 个答案:

答案 0 :(得分:1)

您可以使用以下非正则表达式,因为您知道示例是一个7个字母的单词:

s.split('-')[1][:7]

对于任意单词,它将变为:

s.split('-')[1].split(':')[0]

答案 1 :(得分:1)

您可以使用此正则表达式使用lookarounds捕获-之前和:之后的值

(?<=-).+(?=:)

Regex Demo

Python代码,

import re

str = "http://test-example:123/wd/hub"

print(re.search(r'(?<=-).+(?=:)', str).group())

输出

example

使用非正则表达式的方法是使用这两个拆分

str = "http://test-example:123/wd/hub"

print(str.split(':')[1].split('-')[1])

打印

example

答案 2 :(得分:1)

使用re

import re

text = "http://test-example:123/wd/hub"

m = re.search('(?<=-).+(?=:)', text)
if m:
    print(m.group())

答案 3 :(得分:1)

许多方式

使用拆分:

es_elasticsearch.1.uh1x0s9qr7mb@cluster    | {"type": "server", "timestamp": "2019-04-25T16:28:47,143+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "e8dba5562417",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{e8dba5562417}{Jy3t0AAkSW-jY-IygOCjOQ}{z7MYIf5wTfOhCX1r25wNPg}{10.255.0.46}{10.255.0.46:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }
es_elasticsearch.2.swswlwmle9e9@cluster2    | {"type": "server", "timestamp": "2019-04-25T16:28:47,389+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "af5d88a04b42",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{af5d88a04b42}{zhxMeNMAQN2evKDlsA33qA}{fpYPTvJ6STmyqrgxlMkD_w}{10.255.0.47}{10.255.0.47:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }
es_elasticsearch.3.x8ouukovhh80@master    | {"type": "server", "timestamp": "2019-04-25T16:28:48,818+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "0e7e4d96b31a",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{0e7e4d96b31a}{Xs9966RjTEWvEbuj4-ySYA}{-eV4lvavSHq6JhoW0qWu6A}{10.255.0.48}{10.255.0.48:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }


这很脆弱,如果字符串中有更多的连字符或冒号,则可能会损坏。

使用正则表达式:

example_str = str.split('-')[-1].split(':')[0]

这仍然需要特定的格式,但是更容易适应(如果您知道如何编写正则表达式)。

答案 4 :(得分:1)

我不确定您为什么要从字符串中获取特定单词。我猜你想看看这个单词在给定的字符串中是否可用。

在这种情况下,可以使用下面的代码。

import re
str1 = "http://tests-example:123/wd/hub"
matched = re.findall('example',str1)

答案 5 :(得分:0)

-上分开,然后在:

s = "http://test-example:123/wd/hub"
print(s.split('-')[1].split(':')[0])
#example

答案 6 :(得分:0)

Python字符串具有内置功能find

var s = new AzureStream(blockObject)
ms.CopyTo(s);
s.position = 200;
ms.CopyTo(s);
s.Read...

将返回:

a="http://test-example:123/wd/hub"
b="http://test-exaaaample:123/wd/hub"
print(a.find('example'))
print(b.find('example'))

它是找到的子字符串的索引。如果它等于12 -1 ,则在字符串中找不到子字符串。您还可以使用 in 关键字:

-1

'example' in 'http://test-example:123/wd/hub'