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
Release history Release notifications | RSS feed
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 details)
Built Distribution
File details
Details for the file rgsucher-0.11.tar.gz
.
File metadata
- Download URL: rgsucher-0.11.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0369a418b59c4f57c8713a53b800ef03743adf86597298c2483c48e5cb9b5946 |
|
MD5 | d1d61cc283153c441744e63f21cd189b |
|
BLAKE2b-256 | 5b32fb9ff0bef19c05c2782a0e2a25c2b6ab82d49811b1a22135db9007bd815e |
File details
Details for the file rgsucher-0.11-py3-none-any.whl
.
File metadata
- Download URL: rgsucher-0.11-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32217e9c45212999d1699776d2a0b635d7438f5d2f5bd4a4fe969f1952bf6a85 |
|
MD5 | 89981901eac5057e27bea50f868e008c |
|
BLAKE2b-256 | 408a13aa5319dc69246374f96102ddda6f8ec6d68c6c17f117773d07cde82506 |