java - Java正则表达式用引号数值替换数值
问题描述
有人可以帮忙用正则表达式用单引号替换给定字符串中的所有整数和双精度数:Key1=a,Key2=2,Key3=999.6,Key4=8888,Key5=true
用这个:Key1=a,Key2='2',Key3='999.6',Key4='8888',Key5=true
我想使用正则表达式组捕获规则来替换 = 之后的所有数字字符串 startng 并替换为 ''。
解决方案
您可以在这里尝试使用正则表达式替换所有方法:
String input = "Key1=a,Key2=2,Key3=999.6,Key4=8888,Key5=true";
String output = input.replaceAll("([^,]+)=(\\d+(?:\\.\\d+)?)", "$1='$2'");
System.out.println(output); // Key1=a,Key2='2',Key3='999.6',Key4='8888',Key5=true
以下是使用的正则表达式模式的解释:
([^,]+) match and capture the key in $1
= match =
(\\d+(?:\\.\\d+)?) match and capture an integer or float number in $2
然后,我们替换为$1='$2'
,引用数字值。
推荐阅读
- unity3d - Gitlab-Runner Shell 自动删除文件
- ios - SwiftUI - 此代码是否实例化了一个新的场景对象?
- javascript - 即使我的代码是正确的,为什么当我尝试 npm start 时我的 electron.js 应用程序不会打开?
- python - Python-Bitcoinlib - FileNotFoundError:找不到模块'libeay32'
- c - 拆分C链表而不复制
- python - Linux中Python子进程之间管道的困难
- networking - K8s 是否在普通的 Layer2 网络基础设施上运行?
- c# - 压缩后无效的 C# Zip 文件
- javascript - 我如何让每个人都有不同的频道(实时聊天应用程序)
- python - Telegram Telethon:如果您知道频道哈希,如何获取消息