Home > Software engineering >  Matching Lines with Duplicate String
Matching Lines with Duplicate String

Time:10-01

I need to find lines which contains multiple "-" in the same line leaving single "-" unmatched

INPUT

10 of hearts-2486329.svg
3d-model-3875350.svg
10 of spades-2486331.svg
3d-modeling-2869688.svg

OUTPUT:

3d-model-3875350.svg
3d-modeling-2869688.svg

CodePudding user response:

You're likely overthinking the problem here. If you want two - with any number of characters in between, you can use:

-.*-
  • - Matches a literal -. This is where the match will start.
  • .* Matches any number of any character
  • - Matches a literal - again. This makes the match end at an occurrence of -.
    • Because the * in the .* part is a greedy quantifier, this match will end at the last occurrence of -.

The key thing we've done here is create an expression that only matches on lines with two or more -. Now all we have to do is match to rest of the line. That's easy enough; we can just add .* (any number of any character) to both sides of our expression, and whatever is surrounding our match will be included. Therefore, the final version is:

.*-.*-.*

Make sure to select "Regular expression" and unselect ". matches newline" in the "Search Mode" box in Notepad :

Notepad   search settings

The ". matches newline" setting matters because . means "any character" and if you check that box, it will allow it to match newline characters, too. The problem is that then the .* on both sides of our expression will just match the whole document. We want to restrict the search to one line at a time.

If you don't have Notepad with you at the moment, and you'd like to test this solution out, you can do so at regex101, as Jon P has pointed out.

CodePudding user response:

Try this code, I just tested it

Find: ^((.*[-].*){2})|^(.*\n)
Replace all: $1

  • Related