1

OK, I read through this thread and it hasn't helped me so far.

I have a regex in TextPad (not sure of its engine) like so:

[[:digit:]]+ \= [[:digit:]]+ \+ [[:digit:]]+

that finds a string such as:

1783635 = 1780296 + 3339

and I want to find everything else. I tried encasing the whole expression with [^ expression ] as the TextPad manual says to do, with no luck. I also tried [^][ expression ], ^( expression ), and [^]( expression ), with no luck.

From the thread above, I tried (?! expression ), again, with no luck.

Thoughts?

Community
  • 1
  • 1
hannebaumsaway
  • 2,644
  • 7
  • 27
  • 37

2 Answers2

3

It is not really possible to match "the opposite of" with regex. Regular expressions must match to succeed, they cannot not match and still succeed.

Depending on your exact situation (and TextPad's regex capabilities), there might be a way around this limitation.

More detail is necessary to say that for sure, though. Please provide a real-world text sample and describe what you want to do with it.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • The above includes a real sample. Essentially, that string (`num = num + num`) is within a larger string, and I want to eliminate the rest of the string and extract only the part from above. – hannebaumsaway Sep 19 '11 at 18:46
  • The standard practice in such a case is to match the entire string and replace it with the contents of a match group you define in your expression. How exactly that has to look in your case depends on more detail. – Tomalak Sep 19 '11 at 18:49
  • OK, can you exemplify what you mean? The full string looks like this: `1|dkjasad_fcase1_20110801_001.log|JWE_eggnog_fcase1_20110801_001.log|S| 0.08%|273907 = 273678 + 229` – hannebaumsaway Sep 19 '11 at 18:54
  • @praguian: And that's *one line in your file* you'd like to replace with `273907 = 273678 + 229`? – Tomalak Sep 19 '11 at 18:59
  • Yes, just one line of many like it. I did a short-term solution of compiling the regex to find the "other" part of the line and eliminate it, but in the future the "other" part may not follow a pattern like it does today. That's why I was curious about a way to invert a match instead. – hannebaumsaway Sep 19 '11 at 20:42
  • @praguian: Use `^.*\([[:digit:]]+ \= [[:digit:]]+ \+ [[:digit:]]+\).*`. The `\(...\)` creates group 1, which you can refer to as `\1` in the replacement. Search-and-replace should now remove the unwanted parts. – Tomalak Sep 19 '11 at 20:58
  • Worked, thank you! Is it possible to nest that functionality somehow (have a group within a group)? – hannebaumsaway Sep 21 '11 at 18:38
2

The best way I have found to do this is not with JUST regular expressions, but to use additional functionality from TextPad (bookmarks).

In this case, I needed to identify all lines that did NOT start with PHVS. So I did the following:

  1. Perform "Match All" search with regular expression "^PHVS". This marked every line that started with PHVS

  2. Go to Edit -> Invert Bookmarks. This marked every line that did NOT start with PHVS

  3. Created a macro that pressed F2 (to go to next bookmark) and fix the line the way I needed it

  4. Ran the macro to the end of the file.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51