Skip to content

Commit ac14f1b

Browse files
authored
Merge pull request #1993 from Exirel/find-separator-slash-or-pipe
find: use slash or pipe in sed-like command
2 parents 4c62b80 + 5690063 commit ac14f1b

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

sopel/modules/find.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ def collectlines(bot, trigger):
5050
# Update in-memory list of the user's lines in the channel
5151
line_list = bot.memory['find_lines'][trigger.sender][trigger.nick]
5252
line = trigger.group()
53-
if line.startswith("s/"): # Don't remember substitutions
53+
if line.startswith('s/') or line.startswith('s|'):
54+
# Don't remember substitutions
5455
return
5556
# store messages in reverse order (most recent first)
56-
elif line.startswith("\x01ACTION"): # For /me messages
57+
elif line.startswith('\x01ACTION'): # For /me messages
5758
line = line[:-1]
5859
line_list.appendleft(line)
5960
else:
@@ -113,38 +114,58 @@ def kick_cleanup(bot, trigger):
113114

114115
# Match nick, s/find/replace/flags. Flags and nick are optional, nick can be
115116
# followed by comma or colon, anything after the first space after the third
116-
# slash is ignored, you can escape slashes with backslashes, and if you want to
117-
# search for an actual backslash followed by an actual slash, you're shit out of
118-
# luck because this is the fucking regex of death as it is.
117+
# slash is ignored, and you can use either a slash or a pipe.
118+
# If you want to search for an actual slash AND a pipe in the same message,
119+
# you can escape your separator, in old and/or new.
119120
@plugin.rule(r"""(?:
120-
(\S+) # Catch a nick in group 1
121-
[:,]\s+)? # Followed by colon/comma and whitespace, if given
122-
s/ # The literal s/
123-
( # Group 2 is the thing to find
124-
(?:\\/ | [^/])+ # One or more non-slashes or escaped slashes
125-
)/( # Group 3 is what to replace with
126-
(?:\\/ | [^/])* # One or more non-slashes or escaped slashes
127-
)
128-
(?:/(\S+))? # Optional slash, followed by group 4 (flags)
129-
""")
121+
(?P<nick>\S+) # Catch a nick in group 1
122+
[:,]\s+)? # Followed by optional colon/comma and whitespace
123+
s(?P<sep>/) # The literal s and a separator / as group 2
124+
(?P<old> # Group 3 is the thing to find
125+
(?:\\/|[^/])+ # One or more non-slashes or escaped slashes
126+
)
127+
/ # The separator again
128+
(?P<new> # Group 4 is what to replace with
129+
(?:\\/|[^/])* # One or more non-slashes or escaped slashes
130+
)
131+
(?:/ # Optional separator followed by group 5 (flags)
132+
(?P<flags>\S+)
133+
)?
134+
""")
135+
@plugin.rule(r"""(?:
136+
(?P<nick>\S+) # Catch a nick in group 1
137+
[:,]\s+)? # Followed by optional colon/comma and whitespace
138+
s(?P<sep>\|) # The literal s and a separator | as group 2
139+
(?P<old> # Group 3 is the thing to find
140+
(?:\\\||[^|])+ # One or more non-pipe or escaped pipe
141+
)
142+
\| # The separator again
143+
(?P<new> # Group 4 is what to replace with
144+
(?:\\\||[^|])* # One or more non-pipe or escaped pipe
145+
)
146+
(?:| # Optional separator followed by group 5 (flags)
147+
(?P<flags>\S+)
148+
)?
149+
""")
130150
@plugin.priority('high')
131151
def findandreplace(bot, trigger):
132152
# Don't bother in PM
133153
if trigger.is_privmsg:
134154
return
135155

136156
# Correcting other person vs self.
137-
rnick = Identifier(trigger.group(1) or trigger.nick)
157+
rnick = Identifier(trigger.group('nick') or trigger.nick)
138158

139159
# only do something if there is conversation to work with
140160
history = bot.memory['find_lines'].get(trigger.sender, {}).get(rnick, None)
141161
if not history:
142162
return
143163

144-
old = trigger.group(2).replace(r'\/', '/')
145-
new = trigger.group(3).replace(r'\/', '/')
164+
sep = trigger.group('sep')
165+
old = trigger.group('old').replace('\\%s' % sep, sep)
166+
new = trigger.group('new').replace('\\%s' % sep, sep)
146167
me = False # /me command
147-
flags = (trigger.group(4) or '')
168+
flags = trigger.group('flags') or ''
148169

149170
# If g flag is given, replace all. Otherwise, replace once.
150171
if 'g' in flags:

0 commit comments

Comments
 (0)