0

I'm trying to generate values based on regex defintions using rstr.

Here's what works:

[root@localhost ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>import rstr
>>>print(rstr.xeger(r'[2-9]\d{11}'))
>>>867050869842

I would like now to write a script that would take any regex as an argument and produce an output 10 times:

#!/usr/bin/python3

import sys
import rstr

if len(sys.argv) != 2:
        print("[+] Usage ./regexgen.py regex")
        exit()

regex = sys.argv[1]


for string in range(10):
        print(rstr.xeger(r'{1}')).format(regex)

But when executing the script it fails with the following error's :

[root@localhost ~]# python3 script2.py [2-9]\d{11}
Traceback (most recent call last):
  File "script2.py", line 18, in <module>
    print(rstr.xeger(r'{1}')).format(regex)
  File "/usr/local/lib/python3.6/site-packages/rstr/xeger.py", line 63, in xeger
    parsed = re.sre_parse.parse(pattern)
  File "/usr/lib64/python3.6/sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib64/python3.6/sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "/usr/lib64/python3.6/sre_parse.py", line 616, in _parse
    source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 0

My guess is that xeger is unable to parse the inserted regex with the format function.

0

1 Answer 1

2

Try this:

import sys

import rstr

if len(sys.argv) != 2:
    print("[+] Usage ./regexgen.py regex")
    exit()

regex = sys.argv[1]  # regex passed as arg [2-9]\d{11}
for string in range(10):
    print(rstr.xeger(regex))

Output:

302799318288
303010436356
523231185691
537677558398
824580634154
398638175299
546948835775
845745020055
456616189703
579459609359
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.