带有 2 个下划线和 3 个段的文件名的正则表达式 [英] Regex expression for file name with 2 underscores and 3 segments
问题描述
我需要一个 regex 表达式,该表达式将从属性文件的文件列表中选择具有特定文件名格式的文件.
我需要选择具有以下文件格式的文件名的文件:
I need a regex expression that will select files with particular file name format from the file list of properties files.
I need to select files with file names with following file format:
<app_name>_<app_version>_<environment>.properties
- 这里
可以是任何带有特殊字符
的字母数字,例如abc123
或app1-1
- 这里
可以是任何带有特殊字符
的字母数字,比如abc
甚至 float/integer/string1.0
或2
或 abc1 - 这里
可以是任何带有特殊字符
的字母数字,如production
> 或 prod1 - here
<app_name>
can be any alphanumeric with special character<A-Z/a-z/0-9/special char>
likeabc123
orapp1-1
- here
<app_version>
can be any alphanumeric with special character<A-Z/a-z/0-9/special char/float value>
likeabc
or even float/integer/string1.0
or2
or abc1 - here
<environment>
can be any alphanumeric with special character<A-Z/a-z/0-9/special char>
likeproduction
or prod1
它们一起用 2 个下划线绑定,如下所示:-
Together they are bind with 2 underscore as follows:-
<A-Z/a-z/0-9/special char>_<A-Z/a-z/0-9/special char/float value>_<A-Z/a-z/0-9/special char>.properties
文件名总是包含2个下划线_
,下划线之间可以是任意字符串.
例如,以下是可以选择的有效文件名:
The file name always contains 2 underscore _
, and it can be any string between the underscores .
for examples, following are valid file names that can be selected:
app1_1.0_prod1.properties
app2_2_prod2.properties
app_vers1_prod.properties
app-1_vers1_prod-2.properties
asd_efg_eee.properties
可以是字母或数字或特殊字符或它们之间的组合,下划线之间.
请注意,文件名中只能有 2 个下划线 _
.
除了 2 个下划线 _
之外的任何内容都不是有效的文件名,不会被选中,并且文件名应始终将这 3 个部分用 2 个下划线 _
分隔以下是无效文件名:
It can be letter or number or special char or combination between them, between the underscore .
Please note that it can be only 2 underscore _
in the file name.
Anything other than 2 underscore _
is not a valid file name and will not be selected and the file name should always have these 3 sections separated by 2 underscore _
Following are invalid file name:
abc.properties
abc.123.efg.properties
as_1.efg.ddd.rr.properties
ee_rr.properties
_rr_.properties
我尝试了以下正则表达式:
I tried following regex:
[^_]*\\.[^_].properties
但不工作.也许这是错误的.我没有得到这个的线索.请帮助我创建这个正则表达式.
谢谢
but not working. Maybe this is wrong. I am not getting the clue of getting this.
Please help me in creating this regex.
Thanks
推荐答案
我相信 /^[^_]+_[^_]+_[^_]+\.properties$/
应该满足您的要求:
I believe /^[^_]+_[^_]+_[^_]+\.properties$/
should meet your requirements:
const tests = [
'app1_1.0_prod1.properties',
'app2_2_prod2.properties',
'app_vers1_prod.properties',
'asd_efg_eee.properties',
'abc.properties',
'abc.123.efg.properties',
'as_1.efg.ddd.rr.properties',
'ee_rr.properties',
'_rr_.properties'
];
tests.forEach(test => {
console.log(test, /^[^_]+_[^_]+_[^_]+\.properties$/.test(test));
});
或者,您可以使用 /^([^_]+_){2}[^_]+\.properties$/
如果你想收紧.
的使用,那么我认为你想要
If you want to tighten down on the use of .
, then I think you want
/^[^_.]+_([^_.]+|\d+(\.\d+)?)_[^_.]+\.properties$/
这篇关于带有 2 个下划线和 3 个段的文件名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!