Skip to main content

ripgrep search

Project description

ripgrep search

Tested against Windows / Python 3.11 / Anaconda

pip install rgsucher

from rgsucher import rg_search
import numpy as np

# searching in a folder
wholeregex = r"\d+"
imagefile = r"C:\Users\hansc\Documents\fubax\Disable-IPv6\common"  # folder
server_ip = rg_search(
    regex=wholeregex,
    file_or_folder=imagefile,
    rgexe=r"C:\ProgramData\chocolatey\bin\rg.exe",
    binary=True,
    multiline_dotall=False,
    multiline=False,
    ignore_case=False,
    invert_match=False,
    case_sensitive=False,
    crlf=False,
    word_regexp=False,
    fixed_strings=False,
)

# searching in a file
allregex = [
    r"""\s*<string name=\"server_ip\">\d{3}.\d{3}.\d{3}.\d{3}</string>""",
    r"""\s*<string name=\"Defaultserver\">\d{3}.\d{3}.\d{3}.\d{3}</string>""",
    r"""\s*<string name=\"server_port\">\d{5}</string>""",
    r"""\s*<int name=\"Defaultport\" value=\"\d{5}\" />""",
]
wholeregex = r"(?:(?:" + ")|(?:".join(allregex) + "))"
imagefile = r"C:\ProgramData\BlueStacks_nxt\Engine\Rvc64_42\Data.vhdx"  #file
server_ip = rg_search(
    regex=wholeregex,
    file_or_folder=imagefile,
    rgexe=r"C:\ProgramData\chocolatey\bin\rg.exe",
    binary=True,
    multiline_dotall=False,
    multiline=False,
    ignore_case=False,
    invert_match=False,
    case_sensitive=False,
    crlf=False,
    word_regexp=False,
    fixed_strings=False,
    add_to_cmd="",
)

# replacing the matches using numpy
import re

print(server_ip)
myip = "112.112.131.121"
myport = "55164"
with open(imagefile, "rb") as f:
    imagedata = f.read()
nparray = np.frombuffer(imagedata, dtype=np.uint8).copy()

for k, v in server_ip.items():
    for submatch in v["submatches"]:
        print(submatch)
        original = submatch[0]
        new = re.sub(r">\d{3}.\d{3}.\d{3}.\d{3}<", rf">{myip}<", original)
        new = re.sub(r">\d{4,5}<", rf">{myport}<", new)
        if len(new) > len(original):
            new = new[1:]
        new = re.sub(r'"\d{4,5}"', rf'"{myport}"', new)
        if len(new) > len(original):
            new = new[1:]
        print(new, original)

        replacem = new.encode()
        replacemint = list(replacem)
        coun = 0
        for i in range(submatch[1], submatch[2]):
            nparray[i] = replacemint[coun]
            coun += 1
with open(imagefile, "wb") as f:
    f.write(nparray.tobytes())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rgsucher-0.11.tar.gz (8.0 kB view hashes)

Uploaded Source

Built Distribution

rgsucher-0.11-py3-none-any.whl (8.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page