正则表达式:仅匹配字符的非重复出现

时间:2022-04-29 21:45:06

I need to find and replace all occurrences of apostrophe character in a string, but only if this apostrophe is not followed by another apostrophe.

我需要在字符串中找到并替换所有出现的撇号字符,但前提是这个撇号后面没有其他撇号。

That is

那是

abc'def

abc'def

is a match but

是一场比赛但是

abc''def

abc''def

is NOT a match.

不是匹配。

I've already composed a working pattern - (^|[^'])'($|[^']) but I believe it may be shorter and simpler.

我已经编写了一个工作模式 - (^ | [^'])'($ | [^'])但我相信它可能更短更简单。

Thanks,

谢谢,

Valery

瓦列里

4 个解决方案

#1


9  

depends on your environment - if your environment supports lookahead and lookbehind, you can do this: (?<!')'(?!')

取决于您的环境 - 如果您的环境支持前瞻和后瞻,您可以这样做:(?<!')'(?!')

Ref: http://www.regular-expressions.info/lookaround.html

参考:http://www.regular-expressions.info/lookaround.html

#2


2  

I think your pattern is short and precise. You could be using negative lookahead/lookbehind, but they would make it a lot more complex. Maintainability is important.

我认为你的模式简短而精确。你可能会使用负向前瞻/后视,但它们会使它变得更加复杂。可维护性很重要。

#3


2  

You'll have to be careful for an uneven number of apostrophes:

你需要注意不均匀的撇号:

abc'''def

where you probably do want to replace the 3rd one and leave the 1st and 2nd in there.

你可能想要替换第3个并将第1个和第2个留在那里。

You can do that like this (assuming you already matched string literals and only want to replace the uneven numbered trailing apostrophe):

你可以这样做(假设你已经匹配了字符串文字,只想替换不均匀编号的尾随撇号):

Search for the pattern:

搜索模式:

(('')*)'

and replace it with

并替换它

$1

which is group 1: the even numbered apostrophes (or no apostrophes at all).

这是第1组:偶数撇号(或根本没有撇号)。

I'm not sure what actual problem you're solving, but in case you're parsing/reading a CSV file, or a string that has the likes of CSV input, I highly recommend using a decent CSV parser. Almost all languages have them in some form or another.

我不确定你要解决的是什么实际问题,但是如果你正在解析/读取CSV文件,或者是一个类似CSV输入的字符串,我强烈建议你使用一个不错的CSV解析器。几乎所有语言都以某种形式存在。

#4


0  

see here nagative lookahed q(?!u)

看到这里nagative lookahed q(?!u)

  • (?=pattern) is a positive look-ahead assertion
  • (?=模式)是一个积极的前瞻性断言
  • (?!pattern) is a negative look-ahead assertion
  • (?!pattern)是一个负面的前瞻断言
  • (?<=pattern) is a positive look-behind assertion
  • (?<=模式)是一个积极的后瞻性断言
  • (?<!pattern) is a negative look-behind assertion
  • (?<!pattern)是一个负面的后视断言

http://www.regular-expressions.info/lookaround.html

working DEMO

#1


9  

depends on your environment - if your environment supports lookahead and lookbehind, you can do this: (?<!')'(?!')

取决于您的环境 - 如果您的环境支持前瞻和后瞻,您可以这样做:(?<!')'(?!')

Ref: http://www.regular-expressions.info/lookaround.html

参考:http://www.regular-expressions.info/lookaround.html

#2


2  

I think your pattern is short and precise. You could be using negative lookahead/lookbehind, but they would make it a lot more complex. Maintainability is important.

我认为你的模式简短而精确。你可能会使用负向前瞻/后视,但它们会使它变得更加复杂。可维护性很重要。

#3


2  

You'll have to be careful for an uneven number of apostrophes:

你需要注意不均匀的撇号:

abc'''def

where you probably do want to replace the 3rd one and leave the 1st and 2nd in there.

你可能想要替换第3个并将第1个和第2个留在那里。

You can do that like this (assuming you already matched string literals and only want to replace the uneven numbered trailing apostrophe):

你可以这样做(假设你已经匹配了字符串文字,只想替换不均匀编号的尾随撇号):

Search for the pattern:

搜索模式:

(('')*)'

and replace it with

并替换它

$1

which is group 1: the even numbered apostrophes (or no apostrophes at all).

这是第1组:偶数撇号(或根本没有撇号)。

I'm not sure what actual problem you're solving, but in case you're parsing/reading a CSV file, or a string that has the likes of CSV input, I highly recommend using a decent CSV parser. Almost all languages have them in some form or another.

我不确定你要解决的是什么实际问题,但是如果你正在解析/读取CSV文件,或者是一个类似CSV输入的字符串,我强烈建议你使用一个不错的CSV解析器。几乎所有语言都以某种形式存在。

#4


0  

see here nagative lookahed q(?!u)

看到这里nagative lookahed q(?!u)

  • (?=pattern) is a positive look-ahead assertion
  • (?=模式)是一个积极的前瞻性断言
  • (?!pattern) is a negative look-ahead assertion
  • (?!pattern)是一个负面的前瞻断言
  • (?<=pattern) is a positive look-behind assertion
  • (?<=模式)是一个积极的后瞻性断言
  • (?<!pattern) is a negative look-behind assertion
  • (?<!pattern)是一个负面的后视断言

http://www.regular-expressions.info/lookaround.html

working DEMO