regexnotepad++pcre

Simple Regex failing in Notepad++ (with angle brackets)


A Regex in Notepad++ is not behaving as I expect, and I'm wondering if it's a bug in Notepad++ or something else.

Here is my file:

    <File Name="NAME.EXT">0123456789ABCDEF</File>

Here is my Regex: (\<File .+?\>)

Here are all search settings:

search settings

When I pressed Replace All, here is what I got:

    <File Name
="NAME.EXT">0123456789ABCDEF</File>

Here is what I expected instead:

    <File Name="NAME.EXT">
0123456789ABCDEF</File>

I switched to the Find tab, and even there, it is only matching <File Name

When I load it into regex101.com and selected the "PCRE" flavor (I read that this is what Notepad++ used), it behaves as I expected, not as Notepad++ behaves.


Solution

  • You have to remove the escaping backslashes \.

    Explanation: The documentation states

    \< ⇒ This matches the start of a word using Boost’s definition of words.
    \> ⇒ This matches the end of a word using Boost’s definition of words.

    This is not common use of regular expressions but means that you must not escape the < and > characters to match them.

    Additionally you could remove the group and replace the complete match:

    Find what: <File .+?>

    Replace with: $0\r\n

    This will also get you the result you expect - but if your real input and the real pattern are more complex, the grouping with () might still be relevant.