java - 如何使用正则表达式拆分此字符串“-25+26+78-21”以获得-25、26、78、-21?
问题描述
如何使用正则表达式拆分此字符串“-25+26+78-21”以获得-25、26、78、-21?
解决方案
你可以尝试这样的事情:
//your input
String numbers = "-25+26+78-21";
//split lookahead by + or - and store them in array of strings
//you can do with it afterwards whatever you like, turn it into ints for example
String[] tokens = numbers.split("(?=\\-)|\\+");
System.out.println(Arrays.asList(tokens));
推荐阅读
- javascript - 如何从事件监听器的回调函数中返回一个值
- android - 反应原生图像裁剪
- r - 我需要在一组列中找到最大值,然后将每个单独的记录除以该值。有什么帮助吗?使用聚合?
- mysql - 根据每个员工每周/每月的工作天数计算积分
- reactjs - React Context 和 Storybook:在组件故事中更改 React Context 的值
- google-sheets - 为什么在条件格式公式中引用另一个 Google 表格选项卡不起作用?
- javascript - Next.js 在 url 中有扩展名的外部图像失败
- javascript - 用任何东西填充对象的空值
- c# - 我试图跟随 Brackeys FPS 控制器,但当我试图环顾四周时,我的播放器一直围绕一个固定点运行。为什么?
- css - 仅为 safari 禁用 certian css