Search code examples
searchnotepad++

Mark words in notepad++ including dash (-)


I would like to mark in Notepad++ the sql scripts in a text log. The sql files have this format in the text:

AAAAAAAA.BBBBBBBBBBB.sql

So what I execute is this sentence in search menu:

\w*.sql

As I should get BBBBBBBBBBB.sql. The point is that in some script names there are dashes (-), and when that happens I dont get the whole name, but just the end after the last dash.

For example, in:

AAAAAAAA.BBBBB-CCCCCCC.sql

I would like to get BBBBB-CCCCCCC.sql, but I just get CCCCCCC.sql

Is there any possible formula to get them?


Solution

  • If the match can not start and end with a hyphen:

    \w+(?:-\w+)*\.sql
    
    • \w+ Match 1+ word characters
    • (?:-\w+)* Optionally match - and 1+ word characters
    • \.sql Match .sql

    See a regex demo.

    Note that in your pattern the \w* can also match 0 occurrences and that the . can match any character if it is not escaped.


    Another option could be using a character class to match either - or a word character, but this would also allow to mix and match like --a--.sql

    [\w-]+\.sql
    

    See another regex demo.