13

I am using Notepad++, and I'm trying to find a way where I can search for multiple values on the same line. The regex search is not working for me currently, as I may not know which order the values are in.

For example, I want to search for a line of code which has three specific values. Using .* in the regex search is not working as you must know the order of the values. Is it possible for me to search for these lines without knowing the order of the values?

On each line I have perhaps a trading log which has 30 different tags. I want to search for 35=D, EUR/USD, 150=8. I only want to search for entries with all three values present.

The issue I run into is that the order of these tags are not guaranteed, so the regex search I use below does not always find each entry I require.

35=D.*EUR/USD.*150=8
  • Is there a reason you don't want to use a FIX message parser? – frodoskywalker Aug 22 at 20:42
  • Is this really just a regex question? Seems like the answers would work in most editors with a regex search? – JPhi1618 Aug 23 at 19:11
22

Using lookahead is much more efficient and can deal with any number of alternations without increasing complexity:

  • Ctrl+F
  • "Find what": ^(?=.*\b35=D\b)(?=.*\bEUR/USD\b)(?=.*\b150=8\b).+$
  • Check "Match case"
  • Check "Wrap around"
  • Check "Regular expression"
  • Uncheck ". matches newline"
  • Find All in Current Document

Explanation:

^               # Beginning of line
  (?=           # Start positive lookaead, make sure we have after:
    .*          # 0 or more any character but newline
    \b          # Word boundary to be sure not matching 135=DATA
    35=D        # Literally
    \b          # word boundary
  )             # End lookahead
  (?=           # Start positive lookaead, make sure we have after:
    .*          # 0 or more any character but newline
    \b          # Word boundary
    EUR/USD     # Literally
    \b          # Word boundary
  )             # End lookahead
  (?=           # Start positive lookaead, make sure we have after:
    .*          # 0 or more any character but newline
    \b          # Word boundary
    150=8       # Literally
    \b          # Word boundary
  )             # End lookahead
  .+            # One or more any character but newline
$               # End of line

Screen capture:

Enter image description here

10

To match only those lines where all three strings exist, you can use the alternate | operator and group (...) your patterns to build a regex for all possible permutations:

(35=D.*EUR/USD.*150=8)|(35=D.*150=8.*EUR/USD)|(EUR/USD.*150=8.*35=D)|(EUR/USD.*35=D.*150=8)|(150=8.*35=D.*EUR/USD)|(150=8.*EUR/USD.*35=D)

A readable version with added linebreaks:

(35=D.*EUR/USD.*150=8)|
(35=D.*150=8.*EUR/USD)|
(EUR/USD.*150=8.*35=D)|
(EUR/USD.*35=D.*150=8)|
(150=8.*35=D.*EUR/USD)|
(150=8.*EUR/USD.*35=D)

This would match all lines where a combination of 35=D, EUR/USD and 150=8 (and possible text inbetween) is present. In this sample text, only the last three lines would have a match (button "Find All in Current Document"):

some text 35=D
some text EUR/USD more text 150=8
some text 35=D more text EUR/USD more text 150=8
some text EUR/USD more text 35=D more text 150=8 more text
some text 150=8 more text EUR/USD 35=D more text


screenshot

  • Hello, | is the or operator in notepad++? like || in php and javascript? – user726730 Aug 22 at 7:05
  • hi Freddy, the problem is i want all 3 tag values present in the same line. Using the "|" to separate the patterns doesn't work for me as it will highlight an entry if it is 1 value is present in 1 of the lines. Thanks for looking into this! – Gruff Aug 22 at 7:22
  • Updated to match only lines with all three values. – Freddy Aug 22 at 7:44
  • 2
    @user726730 the | operator is a regular expression feature, nearly all RE libraries (including in PHP and JS) Support that. It is of course something different than boolean or biteise or in those programming languages – eckes Aug 22 at 9:55
  • 1
    @Toto Nobody said that the strings have to be separated by any kind of (word) boundary, so yes... – Freddy Aug 22 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.