Jagger
Jagger

Reputation: 10524

How to exclude from match if a substring of another string

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?

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

Is this at all possible using regular expressions?

Upvotes: 1

Views: 1136

Answers (4)

wvanbergen
wvanbergen

Reputation: 2324

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

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

Upvotes: 0

j_random_hacker
j_random_hacker

Reputation: 51226

/\\t\b/

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

Upvotes: 1

nezroy
nezroy

Reputation: 3186

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.

Upvotes: 0

Adrian Pronk
Adrian Pronk

Reputation: 13906

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(?![\w\\])

Upvotes: 1

Related Questions