繁体   English   中英

Python 正则表达式匹配斜杠前后的字符

[英]Python Regex match character before or after slash

我想编写一个基于以下内容匹配字符串的正则表达式:

  • 字符串在/之前和之后不应有空格
  • 字符串在/之前或之后只能有两个特殊字符 ( * , : )
  • 字符串可以有任意数量的/并且只要没有空格就应该被视为单个匹配项

示例输入看起来像

Artifacts path 'artifacts_tube/AtomInstall*/**' not found.     // 1 match
</root>                                                        // 0 matches
Failed steps: [<DF2 [D:/Users/work/tmp/assets/dummyfailer]>]   // 1 match
Options : *.* /NS /NC /NDL /COPY:DAT /NP /MT:32 /R:11 /W:30    // 0 matches
Copy Dir: D:/Users/tempdir/tmp8fo -> D:/Users/tempdir/tmpj7xj  // 2 matches

我有一个简单的正则表达式,但它不符合上述所有标准\S*\/\S* Output 用于我的正则表达式

Artifacts path 'artifacts_tube/AtomInstall*/**' not found.     // 1 match
</root>                                                        // 1 match
Failed steps: [<DF2 [D:/Users/work/tmp/assets/dummyfailer]>]   // 1 match
Options : *.* /NS /NC /NDL /COPY:DAT /NP /MT:32 /R:11 /W:30    // 8 matches
Copy Dir: D:/Users/tempdir/tmp8fo -> D:/Users/tempdir/tmpj7xj  // 2 matches

这并不完全符合您的要求(它允许:*出现在路径中的任何位置),但我认为您可以通过它实现您的目标。

(?!\/[^:*])(?![^:*]\/)[\w\/*:]*\/[\w\/*:]*

正则表达式 101 演示

这个正则表达式应该可以满足您的需要

[*:](?!\s)\/[\s]*[:*]*

https://regex101.com/r/nI18g9/1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM