Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式替换逗号后保留的数字和数字末尾的符号_Regex_Regexp Replace - Fatal编程技术网

Regex 正则表达式替换逗号后保留的数字和数字末尾的符号

Regex 正则表达式替换逗号后保留的数字和数字末尾的符号,regex,regexp-replace,Regex,Regexp Replace,我想替换逗号后前两位数字后面的所有字符,并将负号保留在字符串末尾 例如,如果字符串为123456789-,则结果应为1234,56- 使用(,\d{2}.*并替换为“$1-”,确实会将所有内容保留到逗号后2位,但不会在字符串末尾保留/添加减号 我尝试过(,\d{2})。*(-)然后用“$1$2”替换,但这也不起作用。如果您想使用替换,可以使用(,\d{2})\d*并替换为$1 (,\d{2}):保留昏迷和所需的两位数 (?:\d*):忽略其他数字 如果您有一个浮点数,最好在上面分组,并根据需

我想替换逗号后前两位数字后面的所有字符,并将负号保留在字符串末尾

例如,如果字符串为123456789-,则结果应为1234,56-

使用(,\d{2}.*并替换为“$1-”,确实会将所有内容保留到逗号后2位,但不会在字符串末尾保留/添加减号


我尝试过(,\d{2})。*(-)然后用“$1$2”替换,但这也不起作用。

如果您想使用替换,可以使用
(,\d{2})\d*
并替换为
$1

  • (,\d{2})
    :保留昏迷和所需的两位数
  • (?:\d*)
    :忽略其他数字

如果您有一个浮点数,最好在上面分组,并根据需要重写该数字,如:

([0-9][0-9]*,[0-9][0-9])[0-9]*([-+])?
^ 1st group   two digits      ^ 2nd group (optional)
然后你可以把它转换成

\1\2

如图所示,只需使用
\d
(,\d{2})\d*