Home > database >  Not getting the first character of the string and get the data which is in a parenthesis using regex
Not getting the first character of the string and get the data which is in a parenthesis using regex

Time:02-05

Main string : 'Netflix|Prime Video|Disney Hotstar|YoutubeOperating System: AndroidUltra HD (4K) 3840 x 2160 Pixels30 W Speaker Output60 Hz Refresh Rate3 x HDMI | 2 x USBA Grade1 Year'

  1. code of regex (OTT): re.findall( r'^\w(.*?)O|U$', Tv_df['Features'][1]) o/p : ['etflix|Prime Video|Disney Hotstar|Youtube']

  2. code of regex (Type): re.findall(r'[a-z] \s(?:HD|SD)', Tv_df['Features'][1]) o/p : ['ltra HD', 'x HD']

  • The output for OTT regex is correct but the first charcter 'N' is missing.
  • The output for type regex should be Ultra HD (4K) . I'm facing the same problem that is the first character 'U' is missing and the data '4K' should be extracted which is in the paranthesis.

P.S : Please find the main string above.

CodePudding user response:

  1. Move the \w inside the ( like: ^(\w.*?)O|U$ to match the first letter.
  2. You are not matching the 'U' of ultra, you can do that by adding [A-Z] at the beginning, and you have to match the things inside ( & ) to get 4K. You can do that by adding \s\(. ?\) to the end. So the pattern will look like this: [A-Z][a-z] \s(?:HD|SD)
  •  Tags:  
  • Related