Need String Comparison 's solution for Partial String Comparison in Python
Question: **Scenario:** I have some tasks performed for respective "Section
Header"(Stored as String), result of that task has to be saved against same
respective "Existing Section Header"(Stored as String)

While mapping if respective task's "Section Header" is one of the "Existing
Section Header" task results are added to it. And if not, new Section Header
will get appended to the Existing Section Header List.

Existing Section Header Looks Like This:

> [ "Activity (Last 3 Days)", "Activity (Last 7 days)", "Executable running
> from disk", "Actions from File"]

For below set of String the expected behaviour is as follows:

"Activity (Last 30 Days) - New Section Should be Added

"Executables running from disk" - Same existing "Executable running from disk"
should be referred [considering extra "s" in Executables same as "Executable".

"Actions from a file" - Same existing "Actions from file" should be referred
[Considering extra article "a"]

Is there any built-in function available python that may help incorporate same
logic. Or any suggestion regarding Algorithm for this is highly appreciated.


Answer: This is a case where you may find [regular
expressions](https://docs.python.org/2/library/re.html) helpful. You can use
[`re.sub()`](https://docs.python.org/2/library/re.html#re.sub) to find
specific substrings and replace them. It will search for _non-overlapping_
matches to a regular expression and repaces it with the specified string.

    
    
    import re #this will allow you to use regular expressions
    
    def modifyHeader(header):
        #change the # of days to 30
        modifiedHeader = re.sub(r"Activity (Last \d+ Days?)", "Activity (Last 30 Days)", header)
        #add an s to "executable"
        modifiedHeader = re.sub(r"Executable running from disk", "Executables running from disk", modifiedHeader)
        #add "a"
        modifiedHeader = re.sub(r"Actions from File", "Actions from a file", modifiedHeader)
    
        return modifiedHeader
    

The `r""` refers to [raw
strings](https://docs.python.org/2/library/re.html#raw-string-notation) which
make it a bit easier to deal with the `\` characters needed for regular
expressions, `\d` matches any digit character, and `+` means "1 or more". Read
the page I linked above for more information.

