- Where Developers Learn, Share, & Build Careers


I am trying to remove some things from a block of text using regex. I have all my patterns ready, but I think the overlapping can not be able to remove two (or more).

For example:

  import r1 = r'i am 'r2 = r'am foo' text = 'I am foo' re.sub (r1, '', Text) # Returns' foo 're.sub (r2,' ', text) # Returns' How do I change both records together and end with an empty string?  

I have finished using a slightly modified version:

  def clean (self, text): pattern Mask for Pattern = Baitier (LAN): To match in the Rifidator (pattern, text): r = Category (match, start), match.and () Mask [R] = 'X' * Lane (R) returns ''. (Character for character, bit in bit (text, mask) in zip)    

You can always call it re.sub Shown can not form. You can use re.finditer to find them all. Each match will give you a match object, in which .start and .end indicate their posts. You can collect them all together, and then finally remove the characters.

Here I use a byteearray as a temporary string, which is used as a mask. It starts for zero bytes, and I mark with an 'x' with all the bytes that match any 'regex'. Then I use bit masks to select the letters to keep in the original string, and create a new string with only one random character:

  bits = byteearray (len ( Text)) Pattern: For re-meters (pat, text): bit [m. Start (): m.end ()] = 'x' * (m.end () - m.start ()) new_string = '' .com (c for c, bit in zip (text, beats) if not Bit)    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -