Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

python - Ignoring cases in a regex split based on surrounding characters

I have a string that I want to split by certain special characters. But I don't want to split anything inside square brackets. How can I set up my regex to ignore cases inside square brackets?

formula = '[var1]+[v/ar/2]^var3/var4' #assume no spaces in the formula
re.split('[-+*/&,^%]',formula) #produces ['[var1]', '[v', 'ar', '2]', 'var3', 'var4']

Desired output:

['[var1]', '[v/ar/2]', 'var3', 'var4']

I think I need to use some fancy negative lookbehind and negative lookahead, but I haven't found a working combination yet.

question from:https://stackoverflow.com/questions/65855881/ignoring-cases-in-a-regex-split-based-on-surrounding-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use split, you can use an alternation to either capture the content that you want to keep after the split in group 1, or match where you want to split on.

In the result, remove the empty strings from the result.

  • ([[^][]*]) Capture group 1, capture from an opening till closing square bracket
  • | Or
  • [-+*/&,^%] Match any of the listed characters you want to split on

Regex demo | Python demo

Example

import re

s="[var1]+[v/ar/2]^var3/var4"
result = list(filter(None, re.split(r"([[^][]*])|[-+*/&,^%]", s)))
print(result)

Output

['[var1]', '[v/ar/2]', 'var3', 'var4']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...