Skip to content

Commit 40df6fe

Browse files
Exireldgw
andcommitted
find: use slash or pipe in sed-like command
Co-authored-by: dgw <[email protected]>
1 parent 00e8c4d commit 40df6fe

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

sopel/modules/find.py

Lines changed: 21 additions & 18 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,20 +114,22 @@ 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're shit out of luck because this is the fucking regex of death as it is.
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+
(\S+) # Catch a nick in group 1
122+
[:,]\s+)? # Followed by colon/comma and whitespace, if given
123+
s([/|]) # The literal s and a separator as group 2 (/ or |)
124+
( # Group 3 is the thing to find
125+
(?:(?!\2).)+ # One or more that isn't the separator from group 2
126+
)
127+
\2 # The separator, from group 2.
128+
( # Group 4 is what to replace with
129+
(?:(?!\2).)* # One or more that isn't the separator from group 2
130+
)
131+
(?:\2(\S+))? # Optional separator from group 2, followed by group 5 (flags)
132+
""")
130133
@plugin.priority('high')
131134
def findandreplace(bot, trigger):
132135
# Don't bother in PM
@@ -141,10 +144,10 @@ def findandreplace(bot, trigger):
141144
if not history:
142145
return
143146

144-
old = trigger.group(2).replace(r'\/', '/')
145-
new = trigger.group(3).replace(r'\/', '/')
147+
old = trigger.group(3)
148+
new = trigger.group(4)
146149
me = False # /me command
147-
flags = (trigger.group(4) or '')
150+
flags = (trigger.group(5) or '')
148151

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

0 commit comments

Comments
 (0)