简体   繁体   English

将正则表达式用于两个定界符以提取字符串

[英]Using regex for two delimiters to extract strings

The question How to delete the words between two delimiters? 问题如何删除两个定界符之间的单词? was really helpful to me. 对我真的很有帮助。

So I have a string like this: 所以我有一个像这样的字符串:

string = $blabla$blav:1085$350_X[7:0]

I am trying to remove whatever is inside the '[]' to make the whole thing be $blabla$blav:1085$350_X . 我试图删除'[]'内部的所有内容,使整个内容变为$blabla$blav:1085$350_X

I tried all these: 我尝试了所有这些:

re.sub('[[^]]+]', '', string)
re.sub(r'[.+?]', '', string)
re.sub('[.*?]', '', string)

Any method to do it with regex involving one step please. 使用正则表达式的任何方法都需要一步。

Also, I need to capture that string [7:0] for later use. 另外,我需要捕获该字符串[7:0]以供以后使用。

Try the regex \\[([0-9]+\\:[0-9]+)\\]$ . 尝试正则表达式\\[([0-9]+\\:[0-9]+)\\]$ It matches [X:Y] where X and Y are numbers and the whole thing is at the end of a string. 它与[X:Y]匹配,其中XY是数字,整个内容位于字符串的末尾。 There is only one group in the regex that returns the two numbers X:Y without the [ and ] 正则表达式中只有一组返回两个数字X:Y而没有[]

Use this to replace the string: 使用它替换字符串:

import re
re.sub('\[([0-9]+\:[0-9]+)\]$', '', string)

You can use this \\[([0-9]+)\\:([0-9]+)\\]$ to match the two numbers in two groups. 您可以使用此\\[([0-9]+)\\:([0-9]+)\\]$来匹配两组中的两个数字。

numbersRegex = re.search('\[([0-9]+)\:([0-9]+)\]$', string)
number1 = numbersRegex.group(1)
number2 = numbersRegex.group(2)
bothNumbers = numbersRegex.group(0)

It is important to use regex instead of just string indexes in case the numbers are two or more digits. 如果数字是两个或多个数字,则使用正则表达式而不是仅使用字符串索引非常重要。 Otherwise, it is fine to indices. 否则,可以建立索引。

If the [X:Y] is not at the end of the string, just remove the $ from the regex. 如果[X:Y]不在字符串末尾,只需从正则表达式中删除$

You can use this website and paste the regex there. 您可以使用此网站并将正则表达式粘贴到此处。 It provides explanation and a text field to test it. 它提供说明和文本字段以进行测试。

You can use rsplit with maxsplit=1 to make sure it only split on last [ , 您可以将rsplitmaxsplit=1一起使用,以确保仅在最后一个[

string = "$blabla$blav:1085$350_X[7:0]"
s_string = string.rsplit('[', maxsplit=1)

left = s_string[0]
right = "[" + s_string[-1]
print(left)
print(right)


# output

$blabla$blav:1085$350_X
[7:0]

If you must use regex, then try positive lookahead to match last occurrence of [ , 如果必须使用正则表达式,请尝试正向查找以匹配上一次出现的[

import re

string = "$blabla$blav:1085$350_X[7:0]"
regex = r'(^.*(?=\[))(.*)'
ss = re.match(regex, string)

left = ss.group(1)
right = ss.group(2)


print(left)
print(right)


# output

$blabla$blav:1085$350_X
[7:0]
string= '$blabla$blav:1085$350_X[7:0]'

cut_string = string.split('[')[0] # = '$blabla$blav:1085$350_X'

bracket_data = string.split('[')[1].replace(']', '') # = '7:0'

Dirty, but it just werks. 脏,但它只是奇怪。

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

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