Проблема с регулярным выражением захвата группы альтернативных строк при использовании Блокнота 2 - PullRequest
1 голос
/ 20 января 2020

У меня есть поиск по регулярному выражению в следующей форме, который отлично работает в приличных текстовых редакторах (таких как VS Code), но не в Notepad2 (это все, что может использовать мой клиент):

http(s)?://www\.(somedomain\.com|otherdomain\.co\.uk|andanotherdomain\.net)

Я разбил его и получил первую группу захвата, работающую с [квадратными скобками]:

http[s]? работает просто отлично - не знаю почему!

… и несмотря на сохранение обычных скобок для второй группа, это работает:

http[s]?://www\.(somedomain\.com)

… но как только вводятся символы канала для альтернативных строк, Notepad2 падает.

Может кто-нибудь помочь, и, возможно, объяснить, почему Notepad 2 нужно что-то другое?


Примечание. На данный момент меня не слишком беспокоит замена. Это шаблон поиска, который выдает ошибку в Notepad2.

1 Ответ

2 голосов
/ 20 января 2020

Похоже, что поиск в регулярных выражениях Notepad2 основан на POSIX BRE, который не поддерживает чередование с некоторыми изменениями. Среди других основных недостатков - отсутствие соответствия между поддержкой разрывов строк.

Все поддерживаемые конструкции регулярного выражения Notepad2 можно проверить в документации Notepad2 4.2.25 :

Regular Expression Syntax

  Note: the Scintilla source code editing component supports only a
  basic subset of regular expression syntax, and searches are limited
  to single lines.

  .      Matches any character.

  (...)  This marks a region for tagging a match.

  \n     Where n is 1 through 9 refers to the first through ninth
         tagged region when replacing. For example, if the search
         string was Fred([1-9])XXX and the replace string was Sam\1YYY,
         when applied to Fred2XXX this would generate Sam2YYY.

  \<     This matches the start of a word.

  \>     This matches the end of a word.

  \x     This allows you to use a character x that would otherwise
         have a special meaning. For example, \[ would be interpreted
         as [ and not as the start of a character set.

  [...]  This indicates a set of characters, for example, [abc] means
         any of the characters a, b or c. You can also use ranges, for
         example [a-z] for any lower case character.

  [^...] The complement of the characters in the set. For example,
         [^A-Za-z] means any character except an alphabetic character.

  ^      This matches the start of a line (unless used inside a set,
         see above).

  $      This matches the end of a line.

  ?      This matches 0 or 1 times. For example, a?b matches ab and b.

  *      This matches 0 or more times. For example, Sa*m matches Sm,
         Sam, Saam, Saaam and so on.

  +      This matches 1 or more times. For example, Sa+m matches Sam,
         Saam, Saaam and so on.

  *?     Causes * and + to behave non-greedy. For example, <.+> matches
  +?     all HTML tags on a line, whereas <.+?> matches only one tag.

  \d     Any decimal digit.
  \D     Any character that is not a decimal digit.

  \s     Any whitespace character.
  \S     Any character that is not a whitespace character.

  \w     Any "word" character.
  \W     Any "non-word" character.

  \xHH   Character with hex code HH.

  -----> Examples (don't use quotes)
         - Quote lines: find "^" replace with "> "
         - Unquote lines: find "^> " replace with ""
         - Remove line numbers: find "^[0-9]+" replace with ""
         - Convert tabs to double spaces: find "\t" replace with "  "
         - Remove NULL bytes: find "\x00" replace with ""
...