具有负前瞻性的PCRE到RE2正则表达式转换

时间:2017-08-30 17:48:08

标签: regex pcre re2

我有一个pcre正则表达式字符串,我正在尝试转换为re2。 这是pcre和要匹配的字符串。

\%(?!$|\W)

它仅匹配%,如果有!或非单词char不

%252525253E%252553Csvg%25252525252525252Fonload%252525252525252525252525252525252525252525252525252525253Dalert(document.domain的)%252525252

结果: %%%%

我最好的转换是:

\%[^!$|\W]

结果:%2%3%3%2%3%3

然而,这与第一个数字匹配,我不希望这样,我希望它的行为与pcre版本完全相同。 这是我测试的地方:

regex-golang DOT appspot DOT com / assets / html / index.html

regex101 DOT com

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

You could try something like this:

$(document).ready(function() {
  "use strict";

  $("#uploadBtn").on("change", function() {

    var files = $(this)[0].files;
    if (!files) return;

    if (files.length > 1) {

      $("#label_span").text(files.length + " files ready to upload");

    } else {

      var filename = files[0].name;
      $("#label_span").text(filename);
    }

  });

});

Since golang doesn't have negative lookahead (at least I think so) you could use a non-capturing group instead.So in this example you will need to use the first capturing group (e.g.matches[1] and not matches[0]) https://regex101.com/r/THTWwB/2

EDIT: A more detailed example in golang to help you understand the above regex is the following:

.uploadFile {
  height: auto;
  padding: 30px;
  background-color: #E0DDDD;
  color: #797979;
}

#uploadBtn {
  display: none;
  width: auto;
}

.uploadButtonLabel {
  font-size: 18px;
  padding: 10px 40px;
  border-style: none;
  margin-top: 10px;
  text-align: center;
}

.uploadButtonLabel:before {
  font-family: fontAwesome;
  content: "\f093";
  margin-right: 10px;
}

.uploadButtonLabel:hover {
  cursor: pointer;
}

In this example you can access your % by using the first capturing group.So for example m[0][0] will be %2 but m[0][1] will be just % (1st capturing group).Note that the first index is the index of the matches.So for the first match is stored in m[0][] , the second in m[1][] etc