Compare a user input to a list python3 - python-3.x

Hey guys so I tried looking at previous questions but they dont answer it like my teacher wants it to be answered. Basically i need to get a string from a user input and see if it has:
at least one of [!,#,#,$,%,^,&,*,(,)] (non-letter and nonnumeric
character)
o Create a list for these special characters
I have no idea how to make a def to do this. Please help!

You should probably look into Regular expressions. Regular expressions allow you to do many string operations in a concise way. Specifically, you'll want to use re.findall() in order to find all special characters in your string and return them. You can check if the returned list has length 0 to check if there were any special characters at all.
With regards to building the regular expression to find special characters itself... I'm sure you can figure that part out ;)

Please try the below
import re
inputstring = raw_input("Enter String: ")
print inputstring
print "Valid" if re.match("^[a-zA-Z0-9_]*$", inputstring) else "Invalid"

Related

How to get a substring with Regex in Python

I am trying to formnulate a regex to get the ids from the below two strings examples:
/drugs/2/drug-19904-5106/magnesium-oxide-tablet/details
/drugs/2/drug-19906/magnesium-moxide-tablet/details
In the first case, I should get 19904-5106 and in the second case 19906.
So far I tried several, the closes I could get is [drugs/2/drug]-.*\d but would return g-19904-5106 and g-19907.
Please any help to get ride of the "g-"?
Thank you in advance.
When writing a regex expression, consider the patterns you see so that you can align it correctly. For example, if you know that your desired IDs always appear in something resembling ABCD-1234-5678 where 1234-5678 is the ID you want, then you can use that. If you also know that your IDs are always digits, then you can refine the search even more
For your example, using a regex string like
.+?-(\d+(?:-\d+)*)
should do the trick. In a python script that would look something like the following:
match = re.search(r'.+?-(\d+(?:-\d+)*)', my_string)
if match:
my_id = match.group(1)
The pattern may vary depending on the depth and complexity of your examples, but that works for both of the ones you provided
This is the closest I could find: \d+|.\d+-.\d+

I don't understand how the following expression works

This is the part of the code I have copied to see the output,
def check(string,sub_str):
if(string.find(sub_str)==-1):
print('no')
else:
print('yes)
# driver code for testing the above function
string='geeks for geeks'
sub_str='geeks'
I specifically wanted to understand how this expression works :
if(string.find(sub_str)==-1): . Also this code is for finding substrings in a given strings can some one tell if this is the optimal way, I know it is tutorial code but I have an easier way to find the substrings. Just wanted to know if that would make passing test cases easier hence the above code. Anyways thanks y'all for your answers.
The method find() returns the index of the string you are looking for. The string in front of find() is the one in which you are looking for the second string.
SentenceThatIsCompletelySearched.find(ForThisPartHere)
If the string you are looking for is present it will return the index (a number on which position of the sentence the string has been found).
If the string is not inside the sentence then find() will return -1 (a number).
So in your case you are checking if sub_str is inside string and if it is not present (return of -1) you will print "no". If it is you will print "yes".

Can I make Python check my list to see if user input contains strings in my list using if statements? If not are there any alternatives?

Beginner with Python but I wanted to use a list to contain multiple potential string responses that the user may give in the input.
def Front_Door():
print("Welcome to the Party Friend!")
Emotional_state = input("How are You Today? ")
Positive_Emotion = ["good", "fine", "happy"]
I tried to use an if statement to get python to check through my list to see if the input contained any of the strings listed in the e.g. I gave.
if Positive_Emotion in Emotional_state:
print("That's Great! Happy to have you here!")
The code still manages to prompt me for Emotional_state but it just repeats the question one more time, if I respond with one of then strings I've listed again it gives me this error:
if Positive_Emotion in Emotional_state:
TypeError: 'in <string>' requires string as left operand, not list
I'm guessing there is a method to make Python search through my list of strings and cross reference it with my inputs and give me the response I want?
Any help is appreciated :).
You are checking if the whole list is in the string!
What you probably want to do is check if any of the items in the list are in the string.
Something like:
if any( [emotion in Emotional_state for emotion in Positive_Emotion] ):
print("That's Great! Happy to have you here!")
This will check each emotion in the list, and if any of them are in the string it will return True.
Hope this helps.
I'm all too late to the party, but here are my two cents.
You only need Positive_Emotion and Emotional_state to switch places in your if statement, so that it becomes:
if Emotional_state in Positive_Emotion:
print("That's Great! Happy to have you here!")

Extracting a substring from a large string in Erlang

I need to search for a substring in a string and return that if it is there in the string.
What is the best way to do that in Erlang? Note that i dont know the place that substring happens in the bigger string so i need to do a search for that.
You can use a regular expression:
> re:run("foobarbaz", "bar", [{capture, first, list}]).
{match,["bar"]}
See the documentation for re:run/3 for more information. In particular you may find that a different capture option suits your need.
Or if you don't need all the features of regular expressions, string:str/2 might be enough:
> string:str(" Hello Hello World World ", "Hello World").
8
This small function may help you. It returns true if the small string can be found in the big string, otherwise it returns false.
string_contains(Big, Small)->
string:str(Big, Small) > 0.

parsing a string that ends

I have a huge string. I need to extract a substring from that that huge string. The conditions are the string starts with either "TECHNICAL" or "JUSTIFY" or "ALIGN" and ends with a number( any number from 1 to 10) followed by period and then followed by space. so for example, I have
string x = "This is a test, again I am testing TECHNICAL: I need to extract this substring starting with testing. 8. This is test again and again and again and again.";
so I need this
TECHNICAL: I need to extract this substring starting with testing.
I was wondering if someone has elegant solution for that.
I was trying to use the regular expression, but I guess I could not figure out the right expresion.
any help will be appreciated.
Thanks in advance.
Try this: #"((?:TECHNICAL|JUSTIFY|ALIGN).*?)(?:[1-9]|10)\. "

Resources