英文:
Regex Only letters but if the name is composed do not remove it
问题
我想删除所有数字,只保留单词,但如果单词是复合词,则不要删除它,例如:
- 输入:
Hola B2B 234234 2.2.2
- 输出:
Hola B2B
也就是说,如果数字在字母后面并形成一个单词,则保留它,但如果数字以字母开头,则删除它。
我有这个,但对我不起作用:https://regexr.com/7dbos
英文:
I want to delete all numbers, just leave the words but if the word is compound then don't delete it, example:
- Input:
Hola B2B 234234 2.2.2
- Output:
Hola B2B
That is, if the number is after the letter and forms a single word then leave it, but if the number begins without a letter then delete it.
I have this but it doesn't work for me https://regexr.com/7dbos
答案1
得分: 1
我认为你想要:
\b([A-Za-z]|\d)A-Za-z\b/g
但你还没有为我指定足够的测试案例,以便我确定你的意图(例如,你想要下划线怎么处理?非ASCII字母怎么办?& 在我提供的规则中允许 "2B2B",但我不知道你是否希望这样。等等。)
英文:
I think you want
\b([A-Za-z]|\d)*[A-Za-z]([A-Za-z]|\d)*\b/g
but you haven't specified enough test cases for me to be sure of your intent (e.g. what do you want to happen with underscores? What about non-ASCII letters? & what I've given here allows "2B2B", but I don't know if you want that. Etc.)
答案2
得分: 1
匹配以数字开头且完全由数字和/或点组成的“单词”:
(^| )\d[.\d]*\b
查看正则表达式实时演示。
将匹配项替换为空白以“删除”它们,留下您想要的字符串。
在前面添加了(^| )
以强制单词边界并消耗前导空格,以避免在处理后,例如"Hola B2B 234234 2.2.2 Amigo"
后留下多个空格,否则会留下"Hola B2B Amigo"
。
英文:
Match "words" that start with a digits and are entirely composed of digits and/or dots:
(^| )\d[.\d]*\b
See regex live demo.
Replace matches with a blank to "delete" them, leaving you with the string you want.
(^| )
added at the front to enforce a word boundary and consume leading spaces to avoid multiple spaces being left after treating, for example "Hola B2B 234234 2.2.2 Amigo"
would otherwise leave "Hola B2B Amigo"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论