Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RE function
#1
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
Reply
#2
\d+M\d+N\d+M
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
thank you so much!!! I really appreciate it.
Reply
#4
https://regex101.com/ will help to test a pattern online.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply
#6
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')]
Reply
#7
i guess instead of s, I will put my directory/ file, right?
Reply
#8
(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
Reply
#9
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
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020