英文:
Golang regex extract from match
问题
我需要使用正则表达式来提取和替换一些文本。
需要替换以下内容的任何单词:
- sub-19 -> u19
- sub19 -> u19
- (sub19) -> u19
我有以下正则表达式,但它缺少一个要求,无法完全解决:
(?i)sub(?:[^a-z|0-9])
英文:
I need to use regex to extract and replace some text.
Any word that has the following needs to be replaced:
- sub-19 -> u19
- sub19 -> u19
- (sub19) -> u19
I have the following but it's missing one of the requirements and can't figure it out 100%
(?i)sub(?:[^a-z|0-9])
答案1
得分: 1
你可以在前后匹配非空白字符,并用第一个捕获组替换。
(?i)\S*sub-?(\d+)\S*
查看正则表达式演示
示例
package main
import (
"regexp"
"fmt"
)
func main(){
var re = regexp.MustCompile(`(?i)\S*sub-?(\d+)\S*`)
var str = `sub-19
sub19
(sub19)`
fmt.Println(re.ReplaceAllString(str, "u$1"))
}
输出
u19
u19
u19
如果你不想删除前导和尾随的非空白字符,可以省略\S*
并添加单词边界\b
(?i)\bsub-?(\d+)\b
查看另一个正则表达式演示
英文:
You can match non whitespace chars before and after and replace with group 1
(?i)\S*sub-?(\d+)\S*
See a regex demo
Example
package main
import (
"regexp"
"fmt"
)
func main(){
var re = regexp.MustCompile(`(?i)\S*sub-?(\d+)\S*`)
var str = `sub-19
sub19
(sub19)`
fmt.Println(re.ReplaceAllString(str, "u$1"))
}
Output
u19
u19
u19
<hr>
If you don't want to remove leading and trailing non whitespace chars, you can omit the \S*
and add a word boundary \b
(?i)\bsub-?(\d+)\b
See another regex demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论