正则表达式-如何也在此正则表达式中包含单个字符?

问题描述 投票:0回答:1

这是我用于本文的正则表达式:

(?![!',:;?\-\d])(\w[A-Za-z']+)

regexp的风格是ECMAScript(JavaScript)

示例文本:

This.Sentence.Has.Some.Funky.Stuff.U.S.S.R.Going.On.And.Contains.Some.   ABBREVIATIONS.Too.

This.Sentence.Has.Some.Funky.Stuff .U.S.S.R. Going.On.And.Contains.Some.   ABBREVIATIONS.Too.

A.S.A.P.?

Ctrl+Alt+Delete  

Mr.Smith bought google.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? A.d.a.m Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't. Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer! He also worked at craigslist.org as a b c d e F G H I J business analyst.

[它正在做我想做的所有事情,但我也无法完成正则表达式以使单个字母与a b c d e F G H I J匹配,在正则表达式中它是[a-zA-Z]

我不希望匹配U.S.A之类的文本,这是我遇到麻烦的地方。

我在How to include character in regular expression处尝试过该解决方案,但由于问题的性质比较复杂,所以我无法解决该问题。

我的任务是用任何东西包装匹配的物品。

以下是同一正则表达式示例的链接:https://regex101.com/r/Qdq4AY/4

regex
1个回答
1
投票

关于您尝试过的模式的一些说明

  • 模式(?![!',:;?\-\d])(\w[A-Za-z']+)将不匹配单个字符,因为由于\w[A-Za-z']+量词,此部分+至少匹配2个字符
  • 否定的前瞻(?!断言右边的内容不是[!',:;?\-\d]中的任何一个,然后与单词char \w匹配,但是\w仅与数字\d匹配,而不与其余部分匹配。

一种选择是匹配您不想保留的内容以捕获您想要保留的内容:

\.?[a-zA-Z](?:\.[a-zA-Z])+\.?|\.[a-zA-Z]\.|(?!\d)(\w[A-Za-z']*)

部分

  • [\.?匹配可选点
  • [[a-zA-Z](?:\.[a-zA-Z])+\.?匹配单个字符a-zA-Z,然后重复1+次一个点以及单个字符和可选点]
  • |
  • [\.[a-zA-Z]\.在2个点之间匹配一个字符a-zA-Z
  • |
  • (?!\d)断言右边的不是数字
  • [(\w[A-Za-z']*)在组1中捕获匹配1+个单词的字符,并重复0+次以上字符类中的任何一个]

Regex demo

例如

const regex = /\.?[a-zA-Z](?:\.[a-zA-Z])+\.?|\.[a-zA-Z]\.|(?!\d)(\w[A-Za-z']*)/g;
const str = `This.Sentence.Has.Some.Funky.Stuff.U.S.S.R.Going.On.And.Contains.Some.   ABBREVIATIONS.Too.
 
This.Sentence.Has.Some.Funky.Stuff .U.S.S.R. Going.On.And.Contains.Some.   ABBREVIATIONS.Too.
 
A.S.A.P.?
 
Ctrl+Alt+Delete
 
Mr.Smith bought google.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? A.d.a.m Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't. Mr. John Johnson Jr. was born in the U.S.A but earned his Ph.D. in Israel before joining Nike Inc. as an engineer! He also worked at craigslist.org as a b c d e F G H I J business analyst.`;
let m;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  if (undefined !== m[1]) {
    console.log(m[1]);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.