hackerrank.com

Practice>Python>Regex and Parsing>Regex Substitution

건이두 2019. 4. 9. 12:58
728x90

hackerrank.com

Regex Substitution

Problem Link : https://www.hackerrank.com/challenges/re-sub-regex-substitution/problem

Difficulty : Medium

 

Tip

  • It's not a simple replacing string
  • You should aware of lookaround, lookahead and lookbegind
  • re.sum(r" && ", ' and ', input())
  • This is not going to work, if input is like ' && && '
  • the space in the middle of two &&
  • it consumed when the first && is matched
  • so second && can't be matched
  • When your pattern is matched, lookaround is included, but the matched string don't have them
  • It is a key of lookaround
  • So, you should write like below
  • re.sub(r"(?<= )(&&)(?= )", 'and', s)
  • re.sub(r"(?<= )(\|\|)(?= )", 'or', s)

Please visit below, for more regarding 'lookaround'.

 

Solution

728x90