简体   繁体   English

用正则表达式替换字符串中首次出现的“,”

[英]Replace first occurrence of the “,” in the string with regex

I have a issue with regex pattern. 我对正则表达式模式有疑问。 It replace not only "," but also all characters that are before. 它不仅替换“,”,而且替换之前的所有字符。

I would like to replace first occurrence of the "," to ".": 我想将“,”的第一次出现替换为“。”:

"1,,000.23" -> "1.,000.23"

This pattern I am using now : 我现在使用的这种模式:

^(.*?),

Result that I receive is : 我收到的结果是:

"1,,000.23" -> ".,000.23"

Expected result : 预期结果 :

"1,,000.23" -> "1.,000.23"

Maybe you could use ^([^,]+), and replace with $1. 也许您可以使用^([^,]+),并替换为$1.

This will capture from the beginning of the string ^ not a comma in a group ([^,]+) and then match a comma , 这将从字符串^的开头捕获而不是组([^,]+)中的逗号,然后匹配一个逗号,

Use ^(.*?), and replace it with $1. 使用^(.*?),并将其替换为$1. . This means: 这意味着:

group as less as possible things from line start till first , into group 1
then match a `,` 
and replace it with the captured text of group 1 `$1` and a dot `.`

See: https://regexr.com/3kjdq 请参阅: https//regexr.com/3kjdq

Copy the capture group to the result. 将捕获组复制到结果。 Replace 更换

^(.*?),

with

$1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM