Jupyter Notebook's search and replace not as greedy as regex101's javascript - regex

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:
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\("[\s\S.]*(", e)
But in Jupyter's find and replace, this only captures up to print("(%s)\n".

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\("[\s\S]*?", e\)
While the dialog shows 0 matches the replacement works anyway:
Note: I've modified your search pattern. The modified dot should match lazy, [\s\S]*? to avoid matching too much. Also, I removed the capture group, it looks like you do not need it.
Update: As it turned out the capture group needed to be inverse to replace the string in question (kudos to xtian):
Search:
(print\("[\s\S.]*)", e
Replace:
$1 " % (e,

Related

STL regex_match failing to match ^.*

I'm an experienced C++ developer, but relatively new to STL. I haven't used STL in any previous projects. I'm trying to use regular expressions to parse the Windows services file (Windows\System32\drivers\etc\services) and my expression works fine in Notepad++ (after I remove the doubled backslashes added for C string literals), but my STL code is failing to match the same expression and text. I've debugged into it, and the best I can tell, I think it's matching the "e" to [A-Za-z], and then failing to match "c" to [^ ]* possibly because it's not seeing the range as negated. I get past the _N_if node in the STL regex code, and then the _N_rep node fails as (nested a few levels deeper in the call stack) it's processing a _N_class node. The _Node=>_Flags value is _Fl_final (4), which does not contain the _Fl_negate (1) flag.
Here, I think, is the essential code I'm trying to debug:
std::basic_regex<TCHAR> re(_T("^([A-Za-z][^ ]*)\\s+(\\d{1,5})\\/(tcp|udp)\\b.*"), std::regex_constants::ECMAScript);
//...
std::match_results<const TCHAR*> mr;
if (std::regex_match(szLine, mr, re))
I believe the content of szLine is
echo 7/tcp
It looks like the "e" gets matched, and then the "c" fails if my debugging skills are good.
I'm using Visual Studio 2013.
Edit: szLine ends with a newline. I wonder if that could be causing the match to fail because . doesn't match newline by default?
It's important to remember when using regex_match that the match must apply to the whole string, including the newline if one exists. Remembering that . does not match a newline by default, the answer here is to add \\n? to the end of the C string literal, which resolves to \n? going into the pattern, which matches an optional newline at the end of the string.
In order to match any symbol including a newline, you can use a [\s\S] (or [\d\D] or even [\w\W]) character class.
Also, your regex wouldn't match the string you provided, as there is no second \d+. Here is a sample code that will work with regex_match:
string line = "echo 7/tcp\n"; // <-- \n is added
string regexStr = "^([A-Za-z][^ ]*)\\s+(\\d{1,5})/(tcp|udp)\\b[\\s\\S]*";
// ^^^^^^^^^
regex rg(regexStr);
smatch sm;
if (regex_match(line, sm, rg)) {
std::cout << sm[1] << std::endl;
std::cout << sm[2] << std::endl;
std::cout << sm[3] << std::endl;
}
See IDEONE demo
Results are:
echo
7
tcp

Notepad++ multi-line regex replacement only replacing first line

I want to comment out a large number of multi-line logging statements with a single regex replace all operation.
My [potentially] multi-line statements look like this:
logger.info( "Log Message: [" + myVariable
+ "] is has a value of: [" + myVariable.getValue() + " ]"
+ LogColors.ANSI_RESET );
The following regex statement (with . matches newline enabled) successfully finds (and selects) all appropriate lines until the ending semicolon.
(logger\..+?\);)
However when I try to replace all with:
// \1
Only the first line of my tagged enclosed expression () is replaced. So I am left with this compiler unfriendly multiline statement:
// logger.info( "Log Message: [" + myVariable
+ "] is has a value of: [" + myVariable.getValue() + " ]"
+ LogColors.ANSI_RESET );
Instead of trying to force everything into a single-line comment, try making it multiline. Replace it with /* \1 */. Your regex is the same, but it eliminates the problem of compilers hating it.
This also has the advantage of keeping the code identical, so that when you uncomment it you're not losing formatting (which often makes the code easier to read).
Note: If this isn't Java, replace /* */ with the relevant multiline.
If you can make a comment like /* sth */ I would suggest:
(?<!/\* )logger\..+?\);(?! \*/)
and replace with:
/* $0 */
See the DEMO

Replace group with spaces

I need to hide part of the string. Hide all before some ending part.
It easy to implement by regexp like this:
replace("123-134-04", ".(?=.*-)", " ")
replace any symbol if future part of string contains "-".
So result is: " -04"
It is important to keep spaces.
But, I can't use lookahead or lookbehind.
I can catch the group before ending part, but how to replace this for right number of spaces?
Or maybe some other ways to resolve this with regex?
Tnanks in advance!
If the number of to be replaced characters does not differ too much, and you have a means to match the part to be preserved, you could run through a series of search and replace:
replace("12-14-04", "^.{5}(-[^-]+)$", " \1")
replace("123-134-04", "^.{7}(-[^-]+)$", " \1")
replace("adfasd-adf-da7474-04", "^.{17}(-[^-]+)$", " \1")
Or you do:
split the string at the position, where the to be preserved part begins,
run the replace("ALL OF THIS SHOULD BECOME BLANKS", ".", " ") on the first part, and
join them up again.

Regular expression for extracting excerpt from long String

I want to extract excerpt from a long string using Regular expression
Example string: "" Is it possible that Germany, which beat Argentina 1-0 today to win the World Cup, that will end up as a loser in terms of economic growth? ""
String to search: " that "
Expected result from regex
" possible that Germany "
" rd Cup, that will end "
I want to search the desired text from the string with -9 and +9 characters from the forward and the backward of the occurence of the searched string. Search string can occur multiple times within the given string.
I am working on an iOS app
using iOS 7.
I have so far created this expression with my little knowledge about reguler expressions but not able to get desired result from that
" (.){0,9} (that) {0,9} "
Remove the spaces in your regex. If you want to capture the matched ones. Then enclose the pattern within capturing groups (ie, ()),
.{9}that.{9}
OR
(?:.{9}|.{0,9})that(?:.{9}|.{0,9})
DEMO
Make the preceding and following characters as optional to match the line which looks like that will change history
Well, in your expression you were just missing the second "." and maybe the "?" for spaces.
.{0,9} ?that ?.{0,9}
Try that.
You can add ( ) for making groups if you want. I added the "?" to make it comply with your other example:
" that will change history"

To extract specific information in the stack trace using regular expression

I encountered a problem at http://regexone.com/example/6? You can view the problem by clicking the link. There was required a regular expression to extract the method name, filename and the line number from a particular stack trace in an android development application.
I used the following regular expression to solve the problem :
at widget.List.([a-zA-Z]+).([A-Za-z]+\.java).(\d{3,4}).
The expression works for the problem but i don't think its the correct regular expression as there is no hat(^) and dollar ($) included in it.
Dot and brackets are a special character in regular expressions and thus should be escaped:
Pattern pattern = Pattern.compile ("at widget\\.List\\.(\\w+)\\((\\w+\\.java):(\\d+)\\)");
Matcher m = pattern.matcher ("...at widget.List.makeView(ListView.java:1727)...");
m.find ();
System.out.println ("Method name: " + m.group (1));
System.out.println ("File name: " + m.group (2));
System.out.println ("Line number: " + m.group (3));
For me output is:
Method name: makeView
File name: ListView.java
Line number: 1727
Here is the regex that can be used to solve this example:
^.*\s+at\s+.*\.(\w+)[(](\w+\.\w+)\:(\d+).*$
You should not use widget.List. in your regex (as you wrote in one of your comments above) as this will match only the classes from the widget.List package.