Python中匹配非空白的正则表达式 [英] Regular expression for matching non-whitespace in Python

查看:36
本文介绍了Python中匹配非空白的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 re.search 来提取第一组非空白字符.我有以下伪脚本重现了我的问题:

I want to use re.search to extract the first set of non-whitespace characters. I have the following pseudoscript that recreates my problem:

#!/usr/bin/env python2.7
import re

line = "STARC-1.1.1.5             ConsCase    WARNING    Warning"
m = re.search('^[^\S]*?',line)
if m:
    print m.group(0)

它似乎在打印空格而不是 STARC-1.1.1.5

It seems to be printing the whitespace instead of STARC-1.1.1.5

据我所知,这个正则表达式是这样说的:在行首找一组非空白字符,不要贪心

So far as I understand it, this regular expression is saying: At the start of the line, find a set of nonwhitespace characters, don't be greedy

我很确定这会奏效,文档 说我可以使用/S 匹配 [] 中的空格,所以我不确定问题出在哪里.

I was pretty sure this would work, the documentation says I can use /S to match whitespace in [], so i'm not sure where the issue is.

现在,我知道,我知道这可能看起来很奇怪,为什么我不使用其他函数来做到这一点?好吧,给猫剥皮的方法不止一种,而且我仍在掌握 Python 中的正则表达式的窍门,所以我想知道如何使用 re.search 以这种方式提取该字段.

Now, I know, I know this probably looks weird, why aren't I using some other function to do this? Well, there's more than one way to skin a cat and i'm still getting the hang of regular expressions in Python so I'd like to know how I can use re.search to extract this field in this fashion.

推荐答案

[^\S] 是一个 否定字符类,等于 \s(空白模式).*? 是一个惰性量词,它匹配零个或多个字符,但尽可能少,当用在模式末尾时,它实际上从不匹配任何字符.

The [^\S] is a negated character class that is equal to \s (whitespace pattern). The *? is a lazy quantifier that matches zero or more characters, but as few as possible, and when used at the end of the pattern never actually matches any characters.

将你的 m = re.search('^[^\S]*?',line) 行替换为

m = re.match(r'\S+',line)

或 - 如果您还想允许空字符串匹配:

or - if you want to also allow an empty string match:

m = re.match(r'\S*',line)

re.match 方法将模式锚定在字符串的开头.使用 re.search,您需要将 ^ 锚点保留在模式的开头:

The re.match method anchors the pattern at the start of the string. With re.search, you need to keep the ^ anchor at the start of the pattern:

m = re.search(r'^\S+',line)

查看 Python 演示:

import re
line = "STARC-1.1.1.5             ConsCase    WARNING    Warning"
m = re.search('^\S+',line)
if m:
    print m.group(0)
# => STARC-1.1.1.5

然而,在这里,在这种情况下,您可能只使用一个 split():

However, here, in this case, you may just use a mere split():

res = line.split() 
print(res[0])

请参阅另一个 Python 演示.

这篇关于Python中匹配非空白的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆