Python Forum

Full Version: RE function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have an urgent question over python programming language.
I am trying to find a pattern in my code with a pattern as follows
intMintNintM
by int, i mean an integer number
this will help me in finding splicing patterns in a DNA RNA complex

I already realize that I need to use the import re function but I do not know how to designate the following pattern.

Please respond as soon as you can.



Thank you
\d+M\d+N\d+M
thank you so much!!! I really appreciate it.
https://regex101.com/ will help to test a pattern online.
I tried using the d+M/d+N/d+M format that you had replied to, but when i ran my program the following was said.
Traceback (most recent call last):
File "C:/Users/hrithikjha/Desktop/SAM output.py", line 6, in <module>
if re.findall(d+M/d+N/d+M):
NameError: name 'd' is not defined
What should I do?
Also, I think that maybe having an older version of python may be the problem.
I have python 2.7.13 IDLE
Please respond as soon as you can and thank you so much.
The expression most be string or always use raw string(r' ') with regex.
>>> import re
>>> s = '222M333N444M'
>>> re.findall(r'\d+M\d+N\d+M', s)
['222M333N444M']

>>> re.findall(r'\d+(M)\d+(N)\d+(M)', s)
[('M', 'N', 'M')]
i guess instead of s, I will put my directory/ file, right?
(Aug-08-2017, 01:30 PM)baronmontesqieu Wrote: [ -> ]i guess instead of s, I will put my directory/ file, right?

no, you will put your string in which you are looking for this pattern
import csv
import re

with open('filename', 'r') as fh:
    reader = csv.reader(fh, delimiter="\t")
    pattern = re.compile('\d+(M)\d+(N)\d+(M)')
    print pattern
Then you need to open the file and read the contents of the file into a string. Then you use the regular expression on the string as snippsat showed.
Pages: 1 2