Jupyter Notebook's search and replace not as greedy as regex101's javascript
Jupyter Notebook's search and replace not as greedy as regex101's javascript
I have a number of logger.*() functions I want to convert to simple print() statements in a Jupyter Notebook. I already changed the beginning of the lines: logger.*(. Now I need to fix the tail and change ", e to " % (e:
logger.*()
print()
logger.*(
", e
" % (e
print("(%s):n"
" Failed to load logger" % (e, ))
logger.error("(%s):n"
" Validation Testing errors occurred. '%s'",
e, report_file)
logger.critical("(%s):n"
" Failed to return Parsed Report "
" in debug mode.", e)
logger.critical("(%s):n"
" Error loading template.", e)
Using Regex101 to test my javascript Regex, I wrote
print("[sS.]*(", e)
But in Jupyter's find and replace, this only captures up to print("(%s)n".
print("(%s)n"
1 Answer
1
The search and replace preview shows only a single line. Nonetheless, regex replace works across multiple lines. Your sample string can be replaced as suggested:
print("[sS]*?", e)
While the dialog shows 0 matches the replacement works anyway:

Note: I've modified your search pattern. The modified dot should match lazy, [sS]*? to avoid matching too much. Also, I removed the capture group, it looks like you do not need it.
[sS]*?
Update: As it turned out the capture group needed to be inverse to replace the string in question (kudos to xtian):
Search:
(print("[sS.]*)", e
Replace:
$1 " % (e,
@xtian I could do multiple multiline replacements. Have you used a lazy match operator as suggested? Please show more of your sample input, if you need help.
– wp78de
Jun 25 at 0:05
Oh! If you don't use lazy then the match skips to the end. With lazy, you get the multiple matches. For some reason I thought lazy only found the first match. I get it. Now with the lazy fix, I see the capture group needed to be inverse, Search:
(print("[sS.]*)", e; Replace: $1 " % (e,. If you update your answer, I'll mark it as correct--you have everything else. One, I didn't get the power of lazy, and two 0 results ain't zero.– xtian
Jun 30 at 13:54
(print("[sS.]*)", e
$1 " % (e,
@xtian highly appreciated I've already upvoted.
– wp78de
Jul 1 at 2:57
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I'm replacing the start and end of a logger with print statements. The start is easy, but the end has common syntax and returns false matches. I need to change the matching ends conditioned on it being a print statement. Curiously, you're right about the match count. Despite showing 0 results, the search did match and correct, but only once and it deleted the rest of the file. haha. So Jupyter fails at multi-line matching and blows up when the match count is 0. weird!
– xtian
Jun 24 at 23:27