如何从匹配中排除另一个字符串的子字符串

时间:2022-09-07 07:29:30

I have a problem. I'd like to match all occurrences of \t in my text (and by \t i mean it literally it is not a tab character) but I would like to exclude a match if it is a part of \t string. How to do that?

我有个问题。我想在我的文本中匹配所有出现的\ t(并且\ t我的意思是字面意思它不是制表符)但是如果它是\ t字符串的一部分我想排除匹配。怎么做?

Example

<HTML>Blah</HTML>\t
D:\\UserData\\tui

I'd like to match \t in the first line but not in second line (as it is a part of \\t).

我想在第一行匹配\ t但在第二行不匹配(因为它是\\ t的一部分)。

Is this at all possible using regular expressions?

这是否可以使用正则表达式?

4 个解决方案

#1


/\\t\b/

\b matches a word boundary (transition from word-like character to non-word-like, or vice versa).

\ b匹配单词边界(从类似字符的字符转换为非字样字符,反之亦然)。

#2


You have to define more precisely what you mean by "part of a string". For example, you might mean: Don't match \t if it is followed by more alphanumerics or slash. So that would become (in Perl):

您必须更准确地定义“字符串的一部分”的含义。例如,您可能意味着:如果后面跟着更多的字母数字或斜杠,请不匹配\ t。这将成为(在Perl中):

  \\t(?![\w\\])

#3


You're going to need to define in exactly which cases a \t should match, and in which ones it shouldn't, before it's possible to determine a regex for it. Your current definition seems to be of the "I'll know it when I see it" variety, which is not sufficient.

在确定正则表达式之前,您需要准确定义\ t应该匹配哪些情况以及哪些情况不应该匹配。你现在的定义似乎是“当我看到它时我就会知道”变种,这还不够。

#4


Another approach: Match anything but a backslash, match a backslash and match a "t" character.

另一种方法:匹配除反斜杠之外的任何内容,匹配反斜杠并匹配“t”字符。

/[^\\](\\t)/

#1


/\\t\b/

\b matches a word boundary (transition from word-like character to non-word-like, or vice versa).

\ b匹配单词边界(从类似字符的字符转换为非字样字符,反之亦然)。

#2


You have to define more precisely what you mean by "part of a string". For example, you might mean: Don't match \t if it is followed by more alphanumerics or slash. So that would become (in Perl):

您必须更准确地定义“字符串的一部分”的含义。例如,您可能意味着:如果后面跟着更多的字母数字或斜杠,请不匹配\ t。这将成为(在Perl中):

  \\t(?![\w\\])

#3


You're going to need to define in exactly which cases a \t should match, and in which ones it shouldn't, before it's possible to determine a regex for it. Your current definition seems to be of the "I'll know it when I see it" variety, which is not sufficient.

在确定正则表达式之前,您需要准确定义\ t应该匹配哪些情况以及哪些情况不应该匹配。你现在的定义似乎是“当我看到它时我就会知道”变种,这还不够。

#4


Another approach: Match anything but a backslash, match a backslash and match a "t" character.

另一种方法:匹配除反斜杠之外的任何内容,匹配反斜杠并匹配“t”字符。

/[^\\](\\t)/