正则表达式以匹配两个单字母单词之间的和符号:

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

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.:

  1. A &B Should be replaced as AB
  2. A& B Should be replaced as AB
  3. A&B Should be replace as AB
  4. ALPHA & B should not be matched. (Since ALPHA is a word)
  5. 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.:

  1. A &B Should be replaced as AB
  2. A& B Should be replaced as AB
  3. A&B Should be replace as AB
  4. ALPHA & B should not be matched. (Since ALPHA is a word)
  5. 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.

huangapple
  • 本文由 发表于 2020年8月1日 19:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204466.html
匿名

发表评论

匿名网友

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

确定