英文:
Regex to match ampersand between two single-letter words
问题
I want to replace ampersand when it is found between two single-letter words.
Given below is the regex I use, but it is matching words consisting or two or more letters, too.
([A-Za-z]{1})((\s&\s)|(&)|(\s&)|(&\s))([A-Za-z]{1})
On top of that, there might be spaces between either of the letters.
E.g.:
- A &B Should be replaced as AB
- A& B Should be replaced as AB
- A&B Should be replace as AB
- ALPHA & B should not be matched. (Since ALPHA is a word)
- ALPHA & BETA Should not be matched. (Since ALPHA & BETA both are words)
英文:
I want to replace ampersand when it is found between two single-letter words.
Given below is the regex I use, but it is matching words consisting or two or more letters, too.
([A-Za-z]{1})((\s&\s)|(&)|(\s&)|(&\s))([A-Za-z]{1})
On top of that, there might be spaces between either of the letters.
E.g.:
- A &B Should be replaced as AB
- A& B Should be replaced as AB
- A&B Should be replace as AB
- ALPHA & B should not be matched. (Since ALPHA is a word)
- ALPHA & BETA Should not be matched. (Since ALPHA & BETA both are words)
答案1
得分: 1
你可以使用以下代码:
String result = s.replaceAll("(\\b[A-Za-z])\\s*&\\s*(?=[A-Za-z]\\b)", "$1");
注意:[A-Za-z]
可以替换为 \\p{Alpha}
(匹配任何ASCII字母)或 \\p{L})
(匹配任何Unicode字母)。
详细信息:
(\b[A-Za-z])
- 第1组:单词边界和一个字母\s*&\s*
- 一个包含0个或多个空白字符的&
(?=[A-Za-z]\b)
- 正向先行断言(允许重叠匹配),要求一个字母后面紧跟一个单词边界,位于当前位置的右侧。
为了保留结果中的第一个单字母词,替换模式包含 $1
占位符。
英文:
You may use
String result = s.replaceAll("(\\b[A-Za-z])\\s*&\\s*(?=[A-Za-z]\\b)", "$1")
Note: [A-Za-z]
may be replaced with \\p{Alpha}
(to match any ASCII letter) or \\p{L})
(to match any Unicode letter).
See the regex demo. Details
(\b[A-Za-z])
- Group 1: a word boundary and a letter\s*&\s*
- a&
enclosed with 0 or more whitespace chars(?=[A-Za-z]\b)
- a positive lookahead (to allow overlapping matche) that requires a letter followed with a word boundary immediately to the right of the current location.
To keep the first one-letter word in the result, the replacement pattern contains $1
placeholder.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论