-2

Lets say i have function that takes string so in code it is called like:

myObject.AddMyString("Something")

I want to search whole solution and find all invokes of .AddMyString() function but i don't want to see it with the same string as parameter more than once. If i search for: .AddMyString\(".*"\) and there are many places when i pass exact string "Something" then when pressing find next it will show many of similar ones. I just want to see all unique strings that are passed to this function in code.

@Edit: I thought i stated my problem clear enough.

Lets say i have code like that:

var myObject = new MyClass();

myObject.AddMyString("Test");
myObject.AddMyString("Computer");
myObject.AddMyString("Printer");

[...]

myObject.AddMyString("Scanner");
myObject.AddMyString("Test");

[...]

myObject.AddMyString("Computer");
myObject.AddMyString("Speakers");

I want to use regex so that i will have only 5 matches. It will match only first occurence with "Test" or "Computer" strings.

When i use regex .AddMyString\(".*"\) it finds 7 matches instead of 5.

kkamil4sz
  • 431
  • 1
  • 5
  • 19
  • I think you need to use an infinte look around for this, see for example this: https://stackoverflow.com/a/45995312/4122889 in fact that particular regex already works 90% towards what you want when i tested it against your sample. – sommmen May 21 '21 at 09:56
  • search with a for "AddMyString" copy the results to a file, iterate that file using a "Set" approach, to keep only the first unique result for each string-file – Mauricio Gracia Gutierrez May 24 '21 at 16:55

1 Answers1

1

Use

(\.AddMyString\(".*?"\))(?![\s\S\r]*\1)

See proof.

EXPLANATION

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    AddMyString              'AddMyString'
--------------------------------------------------------------------------------
    \(                       '('
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    \)                       ')'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [\s\S\r]*                any character of: whitespace (\n, \r,
                             \t, \f, and " "), non-whitespace (all
                             but \n, \r, \t, \f, and " "), '\r'
                             (carriage return) (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \1                       what was matched by capture \1
--------------------------------------------------------------------------------
  )                        end of look-ahead
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37