I'm attempting to pick out strings containing a specific character (*) in an if/else statement using Python's in command. It works in the terminal, but the if statement isn't picking up on it for some reason.
In the terminal:
match = '*moustache'
'*' in match
Out[27]: True
But when I try to use it in an if statement,
if '*' in match == True:
print(match)
does absolutely nothing. Why? Is there a different/better way to do this?
It will work if you remove the == True.
if '*' in match:
print(match)
The if statement will evaluate to True and then the print line will execute.
Related
Here is example of string
Hi {{1}},
The status of your leave application has changed,
Leaves: {{2}}
Status: {{3}}
See you soon back at office by Management.
Expected Result:
Variables Count = 3
i tried python count() using if/else, but i'm looking for sustainable solution.
You can use regular expressions:
import re
PATTERN = re.compile(r'\{\{\d+\}\}', re.DOTALL)
def count_vars(text: str) -> int:
return sum(1 for _ in PATTERN.finditer(text))
PATTERN defines the regular expression. The regular expression matches all strings that contain at least one digit (\d+) within a pair of curly brackets (\{\{\}\}). Curly brackets are special characters in regular expressions, so we must add \. re.DOTALL makes sure that we don't skip over new lines (\n). The finditer method iterates over all matches in the text and we simply count them.
import re
s="facebook.com/https://www.facebook.com/test/"
re.findall("facebook\.com/[^?\"\'>&\*\n\r\\\ <]+?", s)
I only want as a result "facebook.com/test/" ... but I'm getting as a result --
facebook.com/h
facebook.com/t
What's wrong with my RE? I applied the "?" at the end of the expression thinking this would stop greediness, but it's being treated as 0 or 1 expression.
If I remove the "?" I get:
facebook.com/https://www.facebook.com/test/
The non-greedy modifier works forwards but not backwards, which means that when the first instance of facebook.com/ matches it will not be discarded unless the rest of the pattern fails to match, even if it's non-greedy.
To match the last instance of facebook.com/ you can use a negative lookahead pattern instead:
facebook\.com/(?!.*facebook\.com/)[^?\"\'>&\*\n\r\\\ <]+
Demo: https://replit.com/#blhsing/WordyAgitatedCallbacks
I'm pretty new to coding but I though I understood how the 'and' operator worked. In the example I provided I would have thought that the 'False' statement would get run not the 'True' statement. Can someone enlighten me on why this is not performing as I expected?
string = 'asdf'
if 'z' and 's' in string:
True
else:
False
The and keyword is part of an expression and it should be located between two subexpressions. Here you write 'z' and 's' in string which is interpreted as:
('z') and ('s' in string)
where the first subexpression, 'z' is more or less evaluated as True, while the second subexpression is a little more sophisticated (in your example, it is also intereted as True since 's' actually is in string.
Combining both subexpressions yields True (here).
You certainly wanted to write:
if 'z' in string and 's' in string:
Just to build up on the answer above, to get the correct output that you expect from the if statement you need to specify if "z" in string and "s" in string in order for python to compute the correct meaning of what you intend it to do.
string = 'asdf'
if 'z' in string and 's' in string:
print("True")
else:
print("False")
I'm looking for a little help on some Lua. I need some code to match this exact line:
efs.test efs.test.gpg
Here's what I have so far, which matches "efs.test":
if string.match(a.message, "%a+%a+%a+.%%a+%a+%a+%a+") then
print(a.message)
else
print ("Does not match")
end
I've also tried this, which matches:
if string.match(a.message, "efs.test") then
print(a.message)
else
print ("Does not match")
end
But when I try to add the extra text my compiler errors with "Number expected, got string" when running this code:
if string.match(a.message, "efs.test", "efs") then
print(a.message)
else
print ("Does not match")
end
Any pointers would be great!
Thanks.
if string.match(a.message, "%a+%a+%a+.%%a+%a+%a+%a+") then
Firstly, this is a wrong use of quantifiers. From PiL 20.2:
+ 1 or more repetitions
* 0 or more repetitions
- also 0 or more repetitions
? optional (0 or 1 occurrence)
In words, you try to match for unlimited %a+ after you already matched the full word with unlimited %a+
To match efs.test efs.test.gpg - we have 2 filenames I suppose, in a strict sense file names may contain only %w - alphanumeric characters (A-Za-z0-9). This would correctly match efs.test:
string.match(message, "%w+%.%w+")
Going one step further, match efs.test as filename and the following filename:
string.match(message, "%w+%.%w+ %w+%.%w+%.gpg")
While this would match both filenames, you would need to check if matched filenames are the same. We can go one step further yet:
local file, gpgfile = string.match(message, "(%w+%.%w+) (%1%.gpg)")
This pattern will return any <filename> <filename>.gpg where the filenames are equal.
With the use of capture-groups, we capture the filename: it will be returned as the first variable and further represented as %1. Then after the space char, we try to match for %1 (captured filename) followed by .gpg. Since it's also enclosed in brackets, it will become the second captured group and returned as the second variable. Done!
PS: You may want to grab ".gpg" by case-insensitive [Gg][Pp][Gg] pattern.
PPS: File names may contain spaces, dashes, UTF-8 characters etc. E.g. ext4 only forbids \0 and / characters.
string.match optional third argument is the index of the given string to start searching at. If you are looking for exactly efs.test efs.test.gpg in that order with that given spacing, why not just use:
string.match(a.message, "efs%.test efs%.test%.gpg")
If you want to match the entire line containing that substring:
string.match(a.message, ".*efs%.test efs%.test%.gpg.*")
For reference
If you are trying to match that exact line its way easier to just use:
if "efs.test efs.test.gpg" = a.message then
print(a.message)
else
print("string does not match!")
end
Of course this wouldn't find any other strings than this.
Another interpretation I see for your question is that you want to know if it has efs.test in the string, which you should be able to accomplish by doing:
if string.match(a.message, "%w+%.%w+") == "efs.test" then
...
end
Also, look into regex, it's basically the language Lua used to match strings with some exceptions.
Here comes the template, in the while loop , variable "index" is a list, So , I can't understand the code "if index == 0" mean, does index[0] = "suc", index[1]="fail" ? please make it as more clear as possible。
import pexpect
while True:
index = child.expect(["suc","fail",pexpect.TIMEOUT])
if index == 0:
break
elif index == 1:
return False
elif index == 2:
pass #continue to wait
The expect() method returns the index of the pattern that is matched. index is not a list.
According to the manual:
expect(pattern, timeout=-1, searchwindowsize=-1, async=False)
This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several
types. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be
compiled to re types. This returns the index into the pattern list. If the pattern was not a list this returns
index 0 on a successful match. This may raise exceptions for EOF or TIMEOUT. To avoid the EOF or
TIMEOUT exceptions add EOF or TIMEOUT to the pattern list. That will cause expect to match an EOF
or TIMEOUT condition instead of raising an exception.
If you pass a list of patterns and more than one matches, the first match in the stream is chosen. If more
than one pattern matches at that point, the leftmost in the pattern list is chosen.