如何从分隔符之间的字符串中提取值 [英] How to extract values from a string between delimiters

查看:241
本文介绍了如何从分隔符之间的字符串中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从分隔符(/)之间的字符串中提取值,但保留(\ /)



string =a / b\ / c / d< br $> b $ b

预期产量

['a','b \\\ / c','d']



我尝试过:



string =a / b\ / c / d

string.split('/')

['a','b \\\'','c','d']

extract values from a string between delimiters (/) but keep (\/)

string = "a/b\/c/d"

Expected output
['a', 'b\\/c', 'd']

What I have tried:

string = "a/b\/c/d"
string.split('/')
['a', 'b\\', 'c', 'd']

推荐答案

您可以使用正则表达式拆分:

You can use a regular expression split:
import re

string = "a/b\/c/d"
print string.split('/')

pattern = "(?<!\\\\)/"
print re.split(pattern, string)



输出:


Output:

['a', 'b\\', 'c', 'd']
['a', 'b\\/c', 'd']



(?<!\\ \\\\)/ :如果字符串中的当前位置前面没有反斜杠的匹配,则匹配。


(?<!\\\\)/: Matches if the current position in the string is not preceded by a match for the back slash.


这篇关于如何从分隔符之间的字符串中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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