I am having trouble understanding why these two commands differ with one producing the desire results and the other not. An example:
I am truly confused at why "re.sub" doesnt perform a positive lookbehind that re.search can do. It appears to be doing the opposite with the same regex. What is the difference?
Last edited by metallica1973; 01-27-2016 at 08:11 PM..
I am having trouble understanding why these two commands differ with one producing the desire results and the other not.
...
...
I am truly confused at why "re.sub" doesnt perform a positive lookbehind that re.search can do. It appears to be doing the opposite with the same regex. What is the difference?
They are working just as expected. For a moment, disregard the fact that your regex is a look-behind assertion.
The "search" method searches for the pattern and displays it. Since you have greedy search (.*), it displays the string till the end.
The "sub" method substitutes the part of the string that matches the pattern by "nothing" (zero-length string). So, what is left is the remaining part of the string **before the matched pattern** and that is returned.
Here's an example in my python REPL:
I search for a string that starts with "in" and extends as long as it has to i.e. till the end.
The matched part of the string x is in red below:
and that is returned.
Now, if I use the same regex in the "sub()" method, then it means that I want to substitute the matched part by something. Python does the substitution and returns the resultant string. Here's the test with the same string; I substitute the part that matches the regex by "#":
You put a null string instead of '#', hence the matched part got chopped off.
The results of these methods are "opposite" of each other because of the specific regex that you used. It matches a part of the string and goes on till the end. If you replace that by a null string, then the remainder is the part *before* the matched string.
If you had used a non-greedy regex, then the results would not have been "opposite".
An example:
The look-behind assertion simply ensures that the string matching the assertion is not a part of the actual match.
So if my regex is "(?<=a)in.*" then it matches "in" and everything after it, provided it has "a" before it.
But "a" is not part of the match. Hence it is not returned.
And if I substitute the red part above by null string, then the remainder will be "The ra" as seen below:
Again, "a" is not part of the matched string, hence it is not replaced.
Hope that helps.
These 3 Users Gave Thanks to durden_tyler For This Post:
Hi all...
As you know I like making code backwards compatible for as many platforms as possible.
This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
I have a list as follows:
From this i need to grep the element using keyword as "primary" and return output as
12:13-internet-wifi-primary
i used as follows
if (i <= (len(system_info))):
ss = system_info
print... (5 Replies)
Hi all,
I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine..
Below is the code I am trying:
#!/usr/bin/env python
import wmi
c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Hello all,
I have a verilog file as following (part of it):
old.v:
bw_r_rf16x32 AUTO_TEMPLATE (
1957 // .rst_tri_en (mem_write_disable),
1958 .rclk (clk),
1959 .bit_wen (dva_bit_wr_en_e),
1960 .din ... (5 Replies)
I am working on requirement on spreadsheet in python scripting.
I have a spreadsheet containing cell values and with background color.
I am able to read the value value but unable to get the background color of that particular cell.
Actually my requirement is to read the cell value along... (1 Reply)
I need to compare the output files in a directory for sftp, looking through a mask.
Return the full file name.
Eg.
I have a file named locally:
test.txt
I must check through sftp, if a file with the following name:
test_F060514_H173148.TXT
My idea is for the filename to a... (0 Replies)
I am back at it with Python and have run into a little stupid hurdle. My goal is to simply search for the GeoIP.dat database and add the path to a couple of variables. So for example:
geopath=os.system('find /usr/share -iname GeoIP.dat')
geobase = pygeoip.GeoIP(geopath, pygeoip.MEMORY_CACHE)... (3 Replies)
Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here.
I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)