简体   繁体   English

Python正则表达式在字符之前匹配并忽略空格

[英]Python Regex Match Before Character AND Ignore White Space

I'm trying to write a regex to match part of a string that comes before '/' but also ignores any leading or trailing white space within the match. 我正在尝试编写一个正则表达式来匹配'/'之前的字符串的一部分,但也忽略了匹配中的任何前导或尾随空格。

So far I've got ^[^\\/]* which matches everything before the '/' but I can't figure out how to ignore the white space. 到目前为止,我有^[^\\/]*匹配'/'之前的所有内容,但我无法弄清楚如何忽略空格。

      123 / some text 123

should yield 应该屈服

123

and

     a test / some text 123

should yield 应该屈服

a test

That's a little bit tricky. 这有点棘手。 You first start matching from a non-whitespace character then continue matching slowly but surely up to the position that is immediately followed by an optional number of spaces and a slash mark: 首先从非空白字符开始匹配,然后继续慢慢匹配,但肯定会紧接着紧跟可选数量的空格和斜杠标记的位置:

\S.*?(?= *\/)

See live demo here 在这里查看现场演示

If slash mark could be the first non-whitespace character in input string then replace \\S with [^\\s\\/] : 如果斜杠标记可能是输入字符串中的第一个非空白字符,则将\\S替换为[^\\s\\/]

[^\s\/].*?(?= *\/)

This expression is what you might want to explore: 您可能希望探索此表达式:

^(.*?)(\s+\/.*)$

Here, we have two capturing groups where the first one collects your desired output, and the second one is your undesired pattern, bounded by start and end chars, just to be safe that can be removed if you want: 在这里,我们有两个捕获组,第一个收集你想要的输出,第二个是你不需要的模式,由开始和结束字符限制,只是为了安全,如果你想要可以删除:

(.*?)(\s+\/.*)

Python Test Python测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^(.*?)(\s+\/.*)$"

test_str = ("123 / some text 123\n"
    "anything else    / some text 123")

subst = "\\1"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

JavaScript Demo JavaScript演示

 const regex = /^(.*?)(\\s+\\/.*)$/gm; const str = `123 / some text 123 anything else / some text 123`; const subst = `\\n$1`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

RegEx 正则表达式

If this wasn't your desired expression, you can modify/change your expressions in regex101.com . 如果这不是您想要的表达式,您可以在regex101.com中修改/更改表达式。

在此输入图像描述

RegEx Circuit RegEx电路

You can also visualize your expressions in jex.im : 您还可以在jex.im中可视化表达式:

在此输入图像描述

Spaces 空间

For spaces before your desired output, we can simply add a capturing group with negative lookbehind : 对于所需输出之前的空格,我们可以简单地添加一个带有负向lookbehind的捕获组:

 ^(\s+)?(.*?)(\s+\/.*)$

JavaScript Demo JavaScript演示

 const regex = /^(\\s+)?(.*?)(\\s+\\/.*)$/gm; const str = ` 123 / some text 123 anything else / some text 123 123 / some text 123 anything else / some text 123`; const subst = `$2`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

Demo 演示

在此输入图像描述

Here is a possible solution 这是一个可能的解决方案

Regex 正则表达式

(?<!\/)\S.*\S(?=\s*\/)

Example

# import regex # or re

string = ' 123 / some text 123'
test = regex.search(r'(?<!\/)\S.*\S(?=\s*\/)', string)
print(test.group(0))
# prints '123'

string = 'a test / some text 123'
test = regex.search(r'(?<!\/)\S.*\S(?=\s*\/)', string)
print(test.group(0))
# prints 'a test'

Short explanation 简短说明

  • (?<!\\/) says before a possible match there can be no / symbol. (?<!\\/)在可能的匹配之前说没有/符号。
  • \\S.*\\S matches lazily anything ( .* ) while making sure it does not start or end with a white space ( \\S ) \\S.*\\S懒得匹配任何东西.* ),同时确保它不以空格开头或结尾( \\S
  • (?=\\s*\\/) means a possible match must be followed by a / symbol or by white spaces + a / . (?=\\s*\\/)表示可能的匹配必须后跟/符号或空格+ a /

You could do it without a regex 你可以在没有正则表达式的情况下完成它

my_string = "      123 / some text 123"
match = my_string.split("/")[0].strip()

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

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