Name nested regex expression [duplicate] - python

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.

Related

String replace() vs string translate() method [duplicate]

This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?

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 replace a word as long as it satisfies a defined sub string in python [duplicate]

This question already has answers here:
How to find a word that starts with a specific character
(6 answers)
Python - replace all words start with
(2 answers)
Closed 4 years ago.
I'd like to substitute 'diag', 'diagnose' or 'diagnosis' or their typos (i.e. 'diagonoose', etc) to 'diagnose'. It seems all these words share the same pattern 'diag', just wonder how to make a substitute once it satisfies the condition: starts with (or contain) 'diag'.
I have tried the following, however, it doesn't seem to work
re.sub(r"\bdiag$\b","diagnose" , text)

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

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')

What are () (parentheses) are for in regex python [duplicate]

This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here

Categories