-3

I am looking at a list of strings that are space separated. I am trying to find parts of string that contain a lower case "n" letter that is not next to an uppercase letter, then remove the lowercase "n" and any adjacent letter/number. Example:

before = ["23n 5T R3",
"4T 3R 2+ 2-",
"-2 +3RF n3",
"Nn1 L9 3+ n",
"un2 L0 -9 e"]

I am trying to get an output as:

after = ["5T R3",
"4T 3R 2+ 2-",
"-2 +3RF",
"Nn1 O9 3+",
"L0 -9 e"]

I am not exactly sure how to go about starting this regex condition. Apologies if its a bit tough one.

Learner123
  • 87
  • 5
  • Running "[re.sub(r'([\b.n\b|\b.n\b"]+)','', x) for x in before]" removes all lowercase "n" letters, even if adjacent to a capital letter. – Learner123 Dec 18 '20 at 12:38
  • My question is downvoted without guidance on what doesn't look right :) Maybe the admins can provide some input here? – Learner123 Dec 18 '20 at 12:42

1 Answers1

1

You can use negative lookbehind to acheive it.

Demo: https://regex101.com/r/HKi4tp/2

Pattern: \b\S*(?<![A-Z])n\S*\b ?

Breakdown:

  • \b\S*and \S*\b: Match any no of non-space characters at start and end of word. (Note: Based on your need, \S can be replaced with \w or [a-zA-Z0-9])
  • (?<![A-Z])n: match n not preceded by [A-Z]
  • ?: match an optional space after the word
  • In substituition, an empty string will delete it
Prasanna
  • 2,390
  • 10
  • 11