I suggest bookmarking the MSDN Regular Expression Quick Reference
you want to achieve a case insensitive match for the word "rocket" surrounded by non-alphanumeric characters. A regex that would work would be:
\W*((?i)rocket(?-i))\W*
What it will do is look for zero or more (*) non-alphanumeric (\W) characters, followed by a case insensitive version of rocket ( (?i)rocket(?-i) ), followed again by zero or more (*) non-alphanumeric characters (\W). The extra parentheses around the rocket-matching term assigns the match to a separate group. The word rocket will thus be in match group 1.
UPDATE 1:
Matt said in the comment that this regex is to be used in python. Python has a slightly different syntax. To achieve the same result in python, use this regex and pass the re.IGNORECASE option to the compile or match function.
\W*(rocket)\W*
On Regex101 this can be simulated by entering "i" in the textbox next to the regex input.
UPDATE 2 Ismael has mentioned, that the regex is not quite correct, as it might match "1rocket1". He posted a much better solution, namely
(?:^|\W)rocket(?:$|\W)
I suggest bookmarking the MSDN Regular Expression Quick Reference
you want to achieve a case insensitive match for the word "rocket" surrounded by non-alphanumeric characters. A regex that would work would be:
\W*((?i)rocket(?-i))\W*
What it will do is look for zero or more (*) non-alphanumeric (\W) characters, followed by a case insensitive version of rocket ( (?i)rocket(?-i) ), followed again by zero or more (*) non-alphanumeric characters (\W). The extra parentheses around the rocket-matching term assigns the match to a separate group. The word rocket will thus be in match group 1.
UPDATE 1:
Matt said in the comment that this regex is to be used in python. Python has a slightly different syntax. To achieve the same result in python, use this regex and pass the re.IGNORECASE option to the compile or match function.
\W*(rocket)\W*
On Regex101 this can be simulated by entering "i" in the textbox next to the regex input.
UPDATE 2 Ismael has mentioned, that the regex is not quite correct, as it might match "1rocket1". He posted a much better solution, namely
(?:^|\W)rocket(?:$|\W)
I think the look-aheads are overkill in this case, and you would be better off using word boundaries with the ignorecase option,
\brocket\b
In other words, in python:
>>> x="rocket's"
>>> y="rocket1."
>>> c=re.compile(r"\brocket\b",re.I) # with the ignorecase option
>>> c.findall(y)
[]
>>> c.findall(x)
['rocket']
re.match matches at the beginning of the input string.
To match anywhere, use re.search instead.
>>> import re
>>> re.match('a', 'abc')
<_sre.SRE_Match object at 0x0000000001E18578>
>>> re.match('a', 'bac')
>>> re.search('a', 'bac')
<_sre.SRE_Match object at 0x0000000002654370>
See search() vs. match():
Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).
.match() constrains the search to begin at the first character of the string. Use .search() instead. Note too that . matches any character (except a newline). If you want to match a literal period, escape it (\. instead of plain .).
I'm searching for exact word match of the word - "new"
in the string - "This is a new String"
using the regex - /^new$/
But the regex101 does not match anything, what am i missing ?
https://regex101.com/r/o1TBGn/1
Try with specifying the start and end rules in your regex:
re.compile(r'^test-\d+$')
Since Python 3.4 you can use re.fullmatch to avoid adding ^ and $ to your pattern.
>>> import re
>>> p = re.compile(r'\d{3}')
>>> bool(p.match('1234'))
True
>>> bool(p.fullmatch('1234'))
False
For this kind of thing, regexps are very useful :
import re
print(re.findall('\\blocal\\b', "Hello, locally local test local."))
// ['local', 'local']
\b means word boundary, basically. Can be space, punctuation, etc.
Edit for comment :
print(re.sub('\\blocal\\b', '*****', "Hello, LOCAL locally local test local.", flags=re.IGNORECASE))
// Hello, ***** locally ***** test *****.
You can remove flags=re.IGNORECASE if you don't want to ignore the case, obviously.
Below you can use simple function.
def find_word(text, search):
result = re.findall('\\b'+search+'\\b', text, flags=re.IGNORECASE)
if len(result)>0:
return True
else:
return False
Using:
text = "Hello, LOCAL locally local test local."
search = "local"
if find_word(text, search):
print "i Got it..."
else:
print ":("
You should use re.search here not re.match.
From the docs on re.match:
If you want to locate a match anywhere in string, use search() instead.
If you're looking for the exact word 'Not Ok' then use \b word boundaries, otherwise
if you're only looking for a substring 'Not Ok' then use simple : if 'Not Ok' in string.
Copy>>> strs = 'Test result 1: Not Ok -31.08'
>>> re.search(r'\bNot Ok\b',strs).group(0)
'Not Ok'
>>> match = re.search(r'\bNot Ok\b',strs)
>>> if match:
... print "Found"
... else:
... print "Not Found"
...
Found
You could simply use,
Copyif <keyword> in str:
print('Found keyword')
Example:
Copyif 'Not Ok' in input_string:
print('Found string')
You want Python's re module:
>>> import re
>>> regex = re.compile(r"\sthis\s") # \s is whitespace
>>> # OR
>>> regex = re.compile(r"\Wthis\W")
>>> # \w is a word character ([a-zA-Z0-9_]), \W is anything but a word character
>>> str2 = 'researching this'
>>> str3 = 'researching this '
>>> bool(regex.search(str2))
False
>>> regex.search(str3)
<_sre.SRE_Match object at 0x10044e8b8>
>>> bool(regex.search(str3))
True
I have a hunch you're actually looking for the word "this", not "this" with non-word characters around it. In that case, you should be using the word boundary escape sequence \b.
It looks like you want to use regular expressions, but you are using ordinary string methods. You need to use the methods in the re module:
import re
>>> re.search("[^a-z]"+str1+"[^a-z]", str2)
>>> re.search("[^a-z]"+str1+"[^a-z]", str3)
<_sre.SRE_Match object at 0x0000000006C69370>