specify more than one pattern with glob - python [duplicate] - python

This question already has answers here:
Regular expression usage in glob.glob?
(4 answers)
Regular expression, glob, Python
(1 answer)
Closed 4 years ago.
Let's say I have files
bla_foo.txt
bla_Foo.txt
bla_bar.txt
xyz_Bar.txt
foo_bla.txt
is it possible to specify several patterns with glob?
For example how can I select only *Foo.txt, *foo.txt, *bar.txt and *Bar.txt?
I know how to select only *Foo.txt and *foo.txt with glob('*[Ff]oo.txt'). But what if *bar.txt and *Bar.txt are also acceptable? Is this even possible with glob or will I have to create two globs
glob('*[Ff]oo.txt')
glob('*[Bb]ar.txt')

Related

Is it possible to use more than one formatter for the help string of Python's argparse module? [duplicate]

This question already has answers here:
Customize argparse help message
(3 answers)
Argparse and ArgumentDefaultsHelpFormatter. Formatting of default values when sys.stdin/stdout are selected as default
(2 answers)
Closed 2 years ago.
The argparse documentation speaks of four different formatter classes to control the output of the help string to some extend. I find several of them useful.
Can I use several of them together?
E.g. argparse.RawDescriptionHelpFormatter and argparse.ArgumentDefaultsHelpFormatter

Name nested regex expression [duplicate]

This question already has answers here:
Reuse part of a Regex pattern
(6 answers)
Is it possible to define a pattern and reuse it to capture multiple groups?
(1 answer)
Closed 2 years ago.
I'm trying to match strings of the form:
"FLOAT:FLOAT:FLOAT"
where:
FLOAT="(\d*\.\d*)"
I would like to not repeat code. (eg. "(\d*\.\d*):(\d*\.\d*):(\d*\.\d*)")
Is there support for this in regex? Or is this typically done with string manipulation (.format())?
I'm using python, but I'm interested in other flavors of regex as well.

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

Example of Match.expand usage [duplicate]

This question already has answers here:
Use Python Match object in string with backreferences
(1 answer)
Regex in python: is it possible to get the match, replacement, and final string?
(2 answers)
Closed 3 years ago.
What is an example usage of Match.expand ? It doesn't give any examples in the python docs (which is the first I've heard of the method), and only states:
Match.expand(template)
Return the string obtained by doing backslash substitution on the template string template, as done by the sub() method.
How would this actually be used and how could it be useful?
you can find more explanations and examples here:
match.expand
In general:
it allows you to expand the match you have found and modify its' prefix

How to extract the filename from a string using regular expression [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Extract file name with a regular expression
(3 answers)
Get Filename Without Extension in Python
(6 answers)
Extracting extension from filename in Python
(33 answers)
Regex: Get Filename Without Extension in One Shot?
(10 answers)
Closed 3 years ago.
I am new in the regular expression and trying to extract the file name from a string which is basically a file path.
string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes
i know i can do this by below code.
string.split('/')[2].split('.')[0]
but I am looking to do this by using regular expression so if in future the formate of the path changes(input_new/survey/path/path/argentina-attributes.csv) should not affect the output.
i know kind of similar question asked before but I am looking for a pattern which will work for my use case.
Try this,
>>> import re
>>> string = "input_new/survey/argentina-attributes.csv"
Output:
>>> re.findall(r'[^\/]+(?=\.)',string) # or re.findall(r'([^\/]+)\.',string)
['argentina-attributes']
Referred from here
Try this:
string = "input_new/survey/argentina-attributes.csv"
new_string = string.split('/')[-1].split('.')[0]
print(new_string)

Categories