简体   繁体   中英

Python : To list files if my keyword contains using regular expression

Python: (.py file)

I have to pass a property file's path which resides inside a jar in to the classpath while java complile.

vmargs = {
"-DApplicationName" : "myApp",
"-Dport" : "8080",
"-DpropertyFile" : "WEB-INF/library/JARFILE-45.33.jar:/properties/somefile.property"
}

The above code works fine when JARFILE has the version 45.33. But how can i give the path dynamically something like:

"-DpropertyFile" : "WEB-INF/library/JARFILE-(RegEx).jar:/properties/somefile.property"

NB: The version (45.33) can be any number of charector. Example as follows:

Beta-44.55
Beta1-33.33
7777.ee44
44.22222

You can do it with the following

>>> import re
>>> test = "WEB-INF/library/JARFILE-45.33.jar:/properties/somefile.property"
>>> sub = r"WEB-INF/library/JARFILE-[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.jar:/properties/somefile\.property"
>>> print(re.match(sub, test))
<_sre.SRE_Match object at 0x7fd9ab1de718>

Here's the important part:

First we need to ensure that any wildcards (such as ".") are properly escaped, using a backslash.

# old
"WEB-INF/library/JARFILE-45.33.jar:/properties/somefile.property"
# new
"WEB-INF/library/JARFILE-45\.33\.jar:/properties/somefile\.property"

Next you mentioned it can be any character, string or number, so you can use the following set:

[a-zA-Z0-9]

This will match any number or any letter az (upper or lowercase), but only one. By adding a '+' after, it will match 1 or more elements.

We should be able to match our string with our regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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