Konstantin Kuznetsov
Konstantin Kuznetsov

Reputation: 63

Regex to match only the first occurrence on the string in notepad++

When analyzing the logs you often need to find all lines containing some specific word in the log file. The problem is when you do a regular search in notepad++ it returns the same line multiple times, if it contains this word in different positions. To alleviate that I switch to regex search and use the following expression

(.*\K)(text)

Where .* matches the full line, \K discards the selection and then (text) matches the last occurrence of text on the line. This method looks ugly and is not very fast. Is there any better way to do it?

Upvotes: 6

Views: 17830

Answers (1)

user2705585
user2705585

Reputation:

To match only the first occurrence you will have to search many characters from beginning of line, discard that search and look for text that you are looking for.

Following regex does the same.

Regex: (^.*?)\Ktrue true is my text here.

Dummy Input

Log date 12/12/2015
Sr No      desc    amount     status
1          true    $10000     true
2          true    $10000     false
3          true    $10000     true
4          true    $10000     false
5          true    $10000     true

Regex101 Demo

Notepad++ Demo

Notepad++ Demo

Upvotes: 15

Related Questions