0

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())
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Possible duplicate of [Python regular expression match whole word](https://stackoverflow.com/questions/15863066/python-regular-expression-match-whole-word) – KeZ Nov 30 '18 at 20:51
  • What is your desired output? Do you want `['prod','prod']` or just `['prod']` for each string? – rahlf23 Nov 30 '18 at 20:53
  • only prod and not nonprod – user3114051 Nov 30 '18 at 20:54
  • What I'm asking is whether or not you want the `'prod'` portion of the `'nonprod'` word? If not, then you need to use lookbehinds. – rahlf23 Nov 30 '18 at 20:55
  • "Python regular expression match whole word" in this "is" starting and ending with space but in my case there is no space. – user3114051 Nov 30 '18 at 20:56
  • rahlf23, I dont need the portion of word rather return a complete line but if I search for prod line with nonprod should not come. – user3114051 Nov 30 '18 at 21:03

2 Answers2

1

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.

Ricky Kim
  • 1,992
  • 1
  • 9
  • 18
  • So you mean for each type of possible string I have to search possible combination either before or after of desired keyword. Surprise to know there is no sort solution and no one faces this before. – user3114051 Nov 30 '18 at 21:42
  • @user3114051 Yes, because you're looking for a string `prod` not a word `prod`, which usually has spaces around. You still want to match string like `intelprod` right? Then how will it know to match `intelprod` but not `nonprod`? – Ricky Kim Nov 30 '18 at 21:52
  • Yeah, agree. \w*(?<!non)prod\w* or \w*(?<!intel)prod\w* or \w*(?<!intelnon)prod\w* makes sense. – user3114051 Nov 30 '18 at 21:56
0

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

Louis Saglio
  • 1,120
  • 10
  • 20
Mehdi Bahra
  • 319
  • 1
  • 8
  • "_" can work but in some other lines it is like svm_sqldb_prod:gso_sql_sdb1_c2_intelprodsql02_nc2pwsql0350_data_vol01 and svm_sqldb_prod:gso_sql_sdb1_c2_intelnonprodsql02_nc2pwsql0350_data_vol01 – user3114051 Nov 30 '18 at 20:59
  • @user3114051 you can use this regex for that re.search(r"\w(?<!non)prod\w", your_string) , for more information search positive look behind regex – Mehdi Bahra Nov 30 '18 at 21:34