Last active
October 19, 2016 12:56
-
-
Save zonque/29570ec837a5e7022946992adc60c561 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # Find patterns in a text an remove then, along with everything that follows in | |
| # curly braces ({}). The matched pattern may as well contain nested curly brances. | |
| # Patterns may spread across multiple lines. | |
| import sys | |
| import fileinput | |
| def strip_first(s, pattern, count): | |
| out = "" | |
| try: | |
| i = 0 | |
| if count == 0: | |
| i = s.index(pattern) | |
| out = s[:i] | |
| i += len(pattern) | |
| while True: | |
| if i >= len(s): | |
| break | |
| if s[i] == '{': | |
| count += 1 | |
| if s[i] == '}': | |
| count -= 1 | |
| i += 1 | |
| if count == 0: | |
| break | |
| if count == 0: | |
| sub, count = strip_first(s[i:], pattern, count) | |
| out += sub | |
| except ValueError: | |
| out = s | |
| return (out, count) | |
| pattern = sys.argv[1] | |
| count = 0 | |
| for line in sys.stdin: | |
| out, count = strip_first(line, pattern, count) | |
| if len(out) > 0: | |
| sys.stdout.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment