-
Practice>Python>Regex and Parsing>Regex Substitutionhackerrank.com 2019. 4. 9. 12:58728x90
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'hackerrank.com' 카테고리의 다른 글
Practice>Python>Regex and Parsing>Validating phone numbers (0) 2019.04.11 Practice>Python>Regex and Parsing>Validating Roman Numerals (0) 2019.04.11 Practice>Python>Regex and Parsing>Re.start() & Re.end() (0) 2019.04.04 Practice>Python>Regex and Parsing>Re.findall() & Re.finditer() (0) 2019.04.03 Practice>Python>Regex and Parsing>Group(), Groups() & Groupdict() (0) 2019.03.29