英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论