How can I replace matches in a Python regex with a modified version of the match?
I've figured out the answer:
using a lambda function as a repl= variable with re.sub i was capable of modifying the matches and then using them to substitute.
    import re, os, sys
    from pathlib import Path

    #Usage:regs directory
    try:
        if len(sys.argv) == 2:
            folder = sys.argv[1]
            fList = os.listdir(folder)
            print("folder contents: ", end=' ')
            for f in fList:
                if not f == fList[-1]:
                    print(f, end=', ')
                else:
                    print(f, end='.\n\n')
            uInput = input('input a regex: ')
            print()
            regObj = re.compile(f'''{uInput}''')
            wordReg = re.compile(r'''([A-Za-z0-9]+|\s+|[^\w\s]+)''')
            matches = []
            
            for file in fList:
                if os.path.isfile(Path(folder)/Path(file)):
                    currentFileObj = open(f'{folder}/{file}')
                    lines = currentFileObj.readlines()
                    currentFileObj.seek(0)
                    text = currentFileObj.read()
                    words = wordReg.findall(text)
                    matches = list(filter(regObj.match, words))
                    instances = 0
                    print(f"matches in ({file}):\n'", end='')
                    print(regObj.sub(lambda match: "(" + match.group() + ")", text)+"'")
                    for line in lines:
                        matches = regObj.findall(line)
                        for match in matches:
                            print((f"({match})"), end=' ')
                            print(f"in line number {lines.index(line)+1}")
                            if match != '':
                                instances = instances + 1
                    print(f'number of instances found: {instances}\n')
                else:
                    continue
        else:
            print('Usage:regs directory')
    except FileNotFoundError:
        print("that file doesn't exist.")
    except PermissionError:
        print("you don't have permission to search that folder.")

instead of having a loop that goes over the list of the string's words, it just prints the match group between parenthesis like so:
            print(regObj.sub(lambda match: "(" + match.group() + ")", text)+"'")

The output now looks like this.
it also prints the folder contents now.

I wrote this code to search a specific folder's text files for word matches and to specify them:
import re, os, sys
from pathlib import Path

#Usage: regs directory
try:
    if len(sys.argv) == 2:
        folder = sys.argv[1]
        fList = os.listdir(folder)
        uInput = input('input a regex: ')
        regObj = re.compile(f'''{uInput}''')
        wordReg = re.compile(r'''([A-Za-z0-9]+|\s+|[^\w\s]+)''')
        matches = []
        print(fList)

        for file in fList:
            if not os.path.isdir(Path(folder)/Path(file)):
                currentFileObj = open(f'{folder}/{file}')
                content = currentFileObj.readlines()
                currentFileObj.seek(0)
                text = currentFileObj.read()
                words = wordReg.findall(text)
                matches = list(filter(regObj.match, words))
                instances = 0
                print(f"matches in ({file}):\n'", end='')
                for word in words:
                    if word in matches:
                        print("\u0333".join(f"{word} "), end='')
                    else:
                        print(word, end='')
                print("'")
                for line in content:
                    matches = regObj.findall(line)
                    for match in matches:
                        print("\u0333".join(f"{match} "), end=' ')
                        print(f"in line number {content.index(line)+1}")
                        if match != '':
                            instances = instances + 1
                print(f'number of instances found: {instances}\n')
            else:
                continue
    else:
        print('Usage: regs directory')
except FileNotFoundError:
    print("that file doesn't exist.")
except PermissionError:
    print("you don't have permission to search that folder.")

it works for the most part except for a few regular expressions, if the regular expression has punctuation or a white space character next to other characters it wouldn't underline it, it may work if i find out a way to substitute matches with a modified version of the match (replacing the match with an underlined version)
Anyone knows a fix ?
here's what it looks like for any other regex.
you can see in the first text file it doesn't underline the match (out.)
i tried looking for functions that would substitute matches with a modification of said match, doesn't appear like there's any ?
also there's the minor problems of it not being able to underline whitespaces and punctuation properly, and the underline character doesn't appear in the windows7 command prompt, maybe a different character other than the underline can work ?
