gpt4 book ai didi

Java 分割正则表达式

转载 作者:行者123 更新时间:2023-11-30 05:03:21 25 4
gpt4 key购买 nike

如何分割字符串

"-3.0*6.7+(5/2)*-0.8--12.98+4^-0.5"

通过使用正则表达式来

-3.0,*,6.7,+,(,5,/,2,),*,-0.8,-,-12.98,+,4,^,-0.5

最佳答案

使用正则表达式来完成此任务是不切实际的:您最好创建某种标记器/词法分析器来从输入源创建标记。特别是一元减号使得正则表达式分割变得困难。

但是为了回答你的问题,你可以按照以下模式进行划分:

(?=[+*/()^])|(?<=[+*/()^])|(?<=\d-)|(?<=\d)(?=-)

这意味着:

                # Split on:
(?=[+*/()^]) # the empty space that has one of: +, *, /, (, ), ^ ahead of it
| # OR
(?<=[+*/()^]) # the empty space that has one of: +, *, /, (, ), ^ before it
| # OR
(?<=\d-) # the empty space that has a digit followed by a minus sign before it
| # OR
(?<=\d)(?=-) # the empty space that has a digit before it and a minus sign ahead of it

关于Java 分割正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com