Sort by

recency

|

307 Discussions

|

  • + 0 comments

    n = int(input()) pattern = r'(?<= )(&&|||)(?= )' for _ in range(n): a = input() s = re.sub(pattern, lambda m: 'and' if m.group(0) == '&&' else 'or',a) print(s)

  • + 0 comments

    Here is HackerRank Regex Substitution in Python solution - https://programmingoneonone.com/hackerrank-regex-substitution-solution-in-python.html

  • + 0 comments

    and_pattern = "(?<= )\&\&(?= )" or_pattern = "(?<= )||(?= )"

    Explanation: (?<= ) is a look behind positive. This ensures the characters are preceeded by a space. (?= ) is a look ahead positive. This ensures the characters are followed by a space. Finally, the string in question contains characters which themselves are used in regex statement, so they must be escaped with a backslash to be recognized!

  • + 0 comments

    import re

    def replace_operators(text): text = re.sub(r'(?<=\s)||(?=\s)', 'or', text) text = re.sub(r'(?<=\s)&&(?=\s)', 'and', text) return text

    N = int(input()) text = "\n".join(input() for _ in range(N)) result = replace_operators(text) print(result)

  • + 0 comments
    1. import re
      1. for i in range(int(input())):
    2. i = input()
    3. i=re.sub(r"(?<= )&&(?= )","and",i)
    4. i=re.sub(r"(?<= )||(?= )","or",i)
    5. print(i)