how to create regex expression to replace hypen with period with this sample characters HA045141-1 to make HA04514.1

huangapple go评论64阅读模式
英文:

how to create regex expression to replace hypen with period with this sample characters HA045141-1 to make HA04514.1

问题

^([A-Za-z0-9].*?)-[0-9]$

英文:

I have this capture the sample characters.

^([A-Za-z0-9].*?)-[0-9]$

答案1

得分: 0

如果您只需要替换文本中间的连字符,那么可以像这样做:

([A-Za-z0-9].*?)-([0-9])

它会捕获两个组,然后将未捕获的连字符“-”替换为“.”,通过将其替换为“$1.$2”。

这通过获取正则表达式的第一部分,然后将其设置回去,然后在“-”的位置放置一个“.”,最后放置第二个捕获组。

这将匹配“HA045141-1”并将连字符替换为点以生成“HA045141.1”。

英文:

If you just need to replace the hyphen in the middle of the text, then you can do something like this

([A-Za-z0-9].*?)-([0-9])

It takes the two capture groups and then replace the not captured - with ., by replacing it with $1.$2.

This works by taking the first part of the regex and then setting it back, and then putting in a . where the - is placed, and lastly placing the second capture group.

This will provide a match for HA045141-1 and replace the hyphen with a dot to make HA045141.1

huangapple
  • 本文由 发表于 2023年7月3日 14:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定