regexnotepad++text-editor

search pattern in Notepad++


1) First I want to search a text with pattern such as

   app(abs(something),abs(something))

in a large text using Notepad++, a sample of the text shown below:

app(abs(any length of characters here),abs(any length of characters here)),
tapp(abs(any length of characters here),abs(any length of characters here)),
app(abs(any length of characters here),app(any length of characters here)),
app(abs(any length of characters here),some(any length of characters here)),
app(abs(any length of characters here)) ,abs(any length of characters here))

when I use "app(abs((.?)),abs((.?)))" to search it finds first and second line in above sample. The second line is not what I am searching. what is wrong with my expression?

2) If possible ,I want the opened and closed parenthesis ( ) after each "abs" should matched, such as

   "app(     abs(..(..)..),abs(..(..(...)..)..)   )"

but not as

   "app(abs((), abs())"

where first abs has unmatched parenthesis.

Please give some advice!

Thanks in advance


Solution

  • Yes, you should switch Search Mode to Regular expression (at the bottom of Find dialog) and use regular expression as a pattern.

    Assuming that asterisk in your pattern means any single character, you should replace * with . (matches any single character in the regular expression syntax) and put \ before each parenthesis (( and ) are special characters and have to be escaped using \). Thus, you will get:

    str1\(str2\(.....\),str2\(........\)\)
    

    To make it less ugly, you can replace 5 dots with .{5}

    str1\(str2\(.{5}\),str2\(.{8}\)\)
    


    Answer to the first part updated question

    Actualy, pattern above doesn't give the results that you describe. .? matches zero or one any character and parentheses are interpreted as special symbols. Thus, your pattern matches strings like appabsX,abs.

    It should be modified like this:

    app\(abs\((.*)\),abs\((.*)\)\)
    

    it finds first and second line in above sample

    Actually, it finds a part of the second line between t and , and it's correct behavior. If you want to ignore such cases, you should somehow specify the beginning of string you are searching. Some examples:

    ^ matches the begging of line:

    ^app\(abs\((.*)\),abs\((.*)\)\)
    

    (\s+) matches at least one white space character

    (\s+)app\(abs\((.*)\),abs\((.*)\)\)
    

    Also, it would be better to enable lazy matching by putting ? after *, like this:

    ^app\(abs\((.*?)\),abs\((.*?)\)\)