简体   繁体   English

如何存储特殊字符前后的所有内容?

[英]How to store everything before and after a special character?

"t1 = 2; a[0] = 1; a[1] = 2; a[t1] = 3;"

In the above string I want to store all variables and constants separately like:在上面的字符串中,我想分别存储所有变量和常量,例如:

var = ['t1', 'a[0]', 'a[1]', 'a[t1]']

constants = ['2', '1', '2', '3']

Using Regex.使用正则表达式。

Ex:前任:

import re

s = "t1 = 2; a[0] = 1; a[1] = 2; a[t1] = 3;"
var, constants = [], []
for v, c in re.findall(r"(.+?) = (.+?);", s):
    var.append(v)
    constants.append(c)
print(var)
print(constants)

Output: Output:

['t1', ' a[0]', ' a[1]', ' a[t1]']
['2', '1', '2', '3']

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

相关问题 如何删除/替换字符串中第n个特殊字符之前和之后的字符串? - How to remove/replace strings before and after nth special character in a string? 如何删除 Python 中某个字符之前的所有内容 - How to remove everything before certain character in Python 如何在特殊字符和其他所有内容之间插入空格 - How to Insert space between a special character and everything else 如何删除字符串中某个字符后的所有内容? - How to delete everything after a certain character in a string? 如何在python中使用正则表达式直接在特殊字符之后和数字之前找到单词? - How to find the word directly after a special character and before a number using regular expressions in python? 在一个字符之后声明所有内容 - Declare everything after a character 删除特殊字符前后的空白并加入python - Remove White space before and after a special character and join them python 如何在将数据推送到 MSSQL 之前删除所有特殊字符 - How to remove all Special Character before to push the data to MSSQL 如何在特定字符之前从字符串中删除特殊字符? - How to remove special characters from a string before specific character? 如何在Dataframe中最后一次出现字符后删除所有内容? - How to remove everything after the last occurence of a character in a Dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM