从扩展开始的 RTL 查找,直到命中" or =



我有一个处理文件上传的web应用程序。提取文件名后,可以采用等格式

["attachment; filename=","New Microsoft Excel Worksheet.xlsx",""]
["attachment; filename=","/New Microsoft Excel Worksheet.xlsx/",""]
["attachment; filename="jimmy.xlsx""]
["attachment; filename=jimmy.xlsx]

我最初只知道示例2,因此查找了scape斜杠之间的所有字符

(?<=/)(.*?)(?=/)

然而,我需要在上面的例子中输出的东西

New Microsoft Excel Worksheet.xlsx

jimmy.xlsx

即从右到左读取字符串,从超过文件扩展名开始;或=

使用以下正则表达式解决,这要归功于:

https://www.reddit.com/user/CryptoOTC_creator/

[ws]+.[.wd]+

[Character set. Match any character in the set.
w Word. Matches any word character (alphanumeric & underscore).
s Whitespace. Matches any whitespace character (spaces, tabs, line breaks).
] 
+Quantifier. Match 1 or more of the preceding token.
.Escaped character. Matches a "." character (char code 46).
[Character set. Match any character in the set.
.Escaped character. Matches a "." character (char code 46).
w Word. Matches any word character (alphanumeric & underscore).
d Digit. Matches any digit character (0-9).
] 
+Quantifier. Match 1 or more of the preceding token.

模式(?<=/)(.*?)(?=/)只在两个强制性正斜杠之间匹配,而这两个斜杠仅存在于示例字符串中的一个字符串中。

查看您发布的anwser,一种选择可以是将单词字符与介于两者之间的空白字符进行匹配,并匹配一个或多个后面跟着单词字符的点。

w+(?:s+w+)*(?:.w+)+

另一种选择是匹配不允许的字符,并以句点和单词字符结束匹配。

[^/s"=][][^/"=][]*.w+
  • [^/s"=][]使用非空白字符开始匹配,除了/"=[]
  • [^/"=][]*和以前一样,现在允许空白字符
  • .匹配.
  • w+匹配一个或多个单词字符

Regex演示

或者使用捕获组,使匹配更加具体,例如,在末尾省略匹配的点和单词字符。

bfilename=[",/]*([^/s"=][][^/"=][]*)

Regex演示

相关内容

最新更新