python regular expression specific characters, any combination
So I'm trying to create a regex clause where it can detect any combination of 'ACTG' characters, and accept that as valid. While anything else - including a combination of 'ACTG' and some other characters are invalid.
Ultimately, I'll take it out of the while loop, that's just for testing purposes. Right now I believe as long as it starts with either a, c, t, or g, it says it's valid....
Is there a funcition in regex that would be better suited than match?
import re while (True): DnaString = str(input('enter your polynucleotide chain code hooblah')) if (re.match('([ACTG]+[ACTG]*)', DnaString, flags=0)): #if re.search('^ACTG', DnaString) != -1: print ("valid chain.") else: #(re.search('^[ACTG]+[ACTG]*$', DnaString) == -1): print("invalid chain, please check your input.") if (DnaString.find("end") != -1): print("ohokaybye.") break
Why not just
if all(c in 'ACGT' for c in DnaString): # Do success else: # Do failure
Regular Expression HOWTO, If the regex pattern is a string, \w will match all the characters marked as letters in the Unicode database provided by the unicodedata module. regex - python regular expression specific characters, any combination - Stack Overflow. So I'm trying to create a regex clause where it can detect any combination of 'ACTG' characters, and accept that as valid. While anything else - including a combination of 'ACTG' and some other. Stack Overflow. Products.
Your problem is that you are just searching for the ACTG characters anywhere in the string without specifying that nothing else is permitted.
If you change your regex to
^[ACTG]+$ then it will work as expected. The ^ and $ characters are anchors which mean the start and end of the line, respectively.
So the regex above matches a string which contains one or more of the four characters and doesn't allow any other characters either before or after them.
6.2. re — Regular expression operations, This module provides regular expression matching operations similar to If the DOTALL flag has been specified, this matches any character including a newline. Values can be any of the following variables, combined using bitwise OR (the A regular expressionis a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The Python module reprovides full support for Perl-like regular expressions in Python.
If you allow a match to internally repeat acceptable characters, then this might be what you want:
7.2. re — Regular expression operations, This module provides regular expression matching operations similar to If the DOTALL flag has been specified, this matches any character including a newline. Values can be any of the following variables, combined using bitwise OR (the '.' character will match any character without regard to what character it is. The matched character can be an alphabet, number of any special character. By default, period/dot character only matches a single character. To create more meaningful patterns, we can combine it with other regular expression constructs.
re — Regular expression operations, This module provides regular expression matching operations similar to If the DOTALL flag has been specified, this matches any character including a newline. exclusive when used as inline flags, so they can't be combined or follow '-' . While at Dataquest we advocate getting used to consulting the Python documentation, sometimes it’s nice to have a handy PDF reference, so we’ve put together this Python regular expressions (regex) cheat sheet to help you out! This regex cheat sheet is based on Python 3’s documentation on regular expressions.
Regular expressions 1. Special characters, The combination "\w" stands for a "word" character, one of the convenience Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, Returns a match where one of the specified characters (a, r, or n) are present: Try it » [a-n] Returns a match for any lower case character, alphabetically between a and n: Try it » [^arn] Returns a match for any character EXCEPT a, r, and n: Try it »  Returns a match where any of the specified digits (0, 1, 2, or 3) are present: Try
How to write Regular Expressions?, Regular expressions are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java and Python. What is Example : [abc] will match characters a,b and c in any string. ([A-Z]\w+) contains two different elements of the regular expression combined together. The python parser interprets ‘\’ (backslashes) as escape characters in string literals. If the backslash is followed by a special sequence recognized by the parser, the whole escape sequence is replaced by a corresponding special character (for example, ‘ ’ is replaced by a newline character when processed by the parser).
Regular Expression (Regex) Tutorial, Regex is supported in all the scripting languages (such as Perl, Python, PHP, and To match a character having special meaning in regex, you need to use a A Sequence of Characters (or String): Strings can be matched via combining a Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match