Reputation: 33
I know this question has been asked almost hundred times in stack overflow but after doing lot of search and not finding my answer, I am asking this question.
I am looking to search exact word from strings something like below.
'svm_midrangedb_nonprod:svm_midrangedb_nonprod_root'
'svm_midrangedb_prod:svm_midrangedb_prod_root'
I want to search only for 'prod'
but getting both 'prod'
and 'nonprod'
in output.
Here is the code I am using:
re.search(r"\wprod\w", in_volumes.json()[i]['name'].split(":")[2].lower())
Upvotes: 0
Views: 94
Reputation: 2022
You have to make rules to not match nonprod
but match prod
.
For example, maybe you can make it so that if there's n
infront of prod
, you exclude it like this: [^n]prod\w
.
Or maybe some data has n
infront of prod and you want to keep it. Then, you want to exclude if there's non
infront of prod
like this: \w*(?<!non)prod\w*
.
It really depends on the rest of your data and see what kind of rules you can make/apply to them to get your desired data.
Upvotes: 1
Reputation: 319
It's normal because your regular expression tell that you want a string containing "prod"
, in order to solve that very easily you can do the same thing you did but like follow
re.search(r"\w_prod\w", in_volumes.json()[i]['name'].split(":")[2].lower())
I just add a _
character existing in your targeted string
Upvotes: 0