regex - Generating regex to exclude a string in path
问题描述
I'm trying to write a regex which includes all 'component.ts' files which start with 'src' but excludes those files which have 'debug' folder in its file path using
(src\/.*[^((?!debug).)*$]/*.component.ts)
I'm testing the following strings on regex101 tester:
src/abcd/debug/xtz/component/ddd/xyz.component.ts
src/abcd/arad/xtz/xyz.component.ts
Both these strings are giving a perfect match, even though the first one has 'debug' in its path. Where am I going wrong?
解决方案
You are specifying a negative lookahead (?!
in a character class [^((?!debug).)*$]
which would then only match the characters inside the character class.
What you could do is move the negative lookahead to the beginning to assert that what follows is not /debug
or /debug/
:
^(?!.*\/debug\/)src\/.*component\.ts$
Explanation
^
Assert the start of the line(?!.*\/debug\/)
Negative lookahead to assert that what follows is not/debug/
src
Match literally\/.*component\.ts
Match a forward slash followed by any character zero or more times followed by.ts
$
Assert the end of the string
Note that to match the dot literally you have to escape it \.
or else it would match any character.
推荐阅读
- php - 使用 PHP,如何用标签替换标签?
- sql - 想要运行一个查询,该查询使用表中的字段作为它的条件(想要循环)
- windows - 将所有管理员的读取权限添加到复制的文件
- android - 空指针异常 mapview.onResume
- javascript - 如果数据字段有值,显示块分隔div
- swift - 关于清洁的常用方法
- algorithm - 如何计算元素的所有可能子集加起来等于给定数量的重复元素
- hyperledger-fabric - 未创建订购者
- javascript - 在反应原生中链接谷歌地图应用程序时如何呈现方向?
- python - VTK ObbTree.IntersectWIthLine 对于大规模碰撞检测来说太慢了