Library to generate random strings from regular expressions.

Overview
https://travis-ci.org/crdoconnor/xeger.svg?branch=master

Xeger

Library to generate random strings from regular expressions.

To install, type:

pip install xeger

To use, type:

>>> from xeger import Xeger
>>> x = Xeger(limit=10)  # default limit = 10
>>> x.xeger("/json/([0-9]+)")
u'/json/15062213'

About

Code adapted and refactored from the Python library rstr by Leapfrog Online (now iProspect), in turn inspired by the Java library Xeger.

Comments
  • Handle regex qualifier incoherent with Xeger limit

    Handle regex qualifier incoherent with Xeger limit

    Handle regex qualifier incoherent with Xeger limit. Useful when mixing greedy operator with small limit (ex: limit=5) and explicit qualifier like {12,16}. Fix the issue #6 .

    I do think it should not raise an assertion error, probably a warning log would be useful.

    opened by poussik 2
  • Added a random instance to the Xeger object

    Added a random instance to the Xeger object

    Seems like Xeger can benefit from holding its own random instance. One way that it is useful is the capability of seeding Xeger so that it can reproduce the same string given the same regex.

    Changes

    • Xeger now calls its own random instance for random generation
    • Xeger now support a .seed(n) function to seed the instance
    • added test scenario for separate, seeded Xeger instance to generate the same string
    opened by ghosalya 1
  • Broken on python 3.5

    Broken on python 3.5

    xeger is completely unusable with python 3.5.

    Traceback (most recent call last):
      File "/home/kapsh/tmp/xeger/xeger/tests/test_xeger.py", line 119, in test_zero_or_more_non_greedy
        self.match(r'a*?')
      File "/home/kapsh/tmp/xeger/xeger/tests/test_xeger.py", line 11, in match
        assert re.match(pattern, xeger.xeger(pattern))
      File "/home/kapsh/tmp/xeger/xeger/xeger.py", line 82, in xeger
        result = self._build_string(parsed)
      File "/home/kapsh/tmp/xeger/xeger/xeger.py", line 89, in _build_string
        newstr.append(self._handle_state(state))
      File "/home/kapsh/tmp/xeger/xeger/xeger.py", line 94, in _handle_state
        return self._cases[opcode](value)
    KeyError: MIN_REPEAT
    

    Unit test output: py35tests.txt

    opened by kapsh 1
  • Please consider moving from github.

    Please consider moving from github.

    Please consider moving from github. Now that it his in the hands of Microsoft there is no way to trust anything stored here, and we will need Servo to make a safe browser since Mozilla now outsource to Microsoft.

    opened by microsoftisevil 0
  • Seed 0 is treated as

    Seed 0 is treated as "no seed given"

    Running

    def test_xeger():
        assert Xeger(seed=0).xeger('.{1,8}') == Xeger(seed=0).xeger('.{1,8}')
    

    fails with an assertion error, since seed 0 is treated as "no seed given"

    opened by artamonovkirill 0
  • Invalid input generated for

    Invalid input generated for "^(?!\s*$).+$" pattern

    E.g., the following test fails:

    def test_non_empty_string():
        pattern = r"^(?!\s*$).+$"
        xg = xeger.Xeger(seed=336)
        generated = xg.xeger(pattern)
        assert re.match(pattern, generated)
    

    (since Xeger generates "\t")

    opened by artamonovkirill 0
  • Is it possible to use it in a sequential way?

    Is it possible to use it in a sequential way?

    I found this really useful, but I'm trying to get strings sequentially to perform some brute force actions. Is it possible to do it? Of course using regex with limited options (not an: a* for example)

    Thank you!

    opened by ArgiesDario 1
  • Regexp raises a KeyError

    Regexp raises a KeyError

    The regexp below works with rstr.xeger but with xeger it raises "KeyError: CATEGORY_DIGIT".

    In [44]: re = r'(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M'
    
    In [45]: rstr.xeger(re)
    Out[45]: '05:55:17 PM'
    
    In [46]: xeger.Xeger().xeger(re)
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-46-28a9a27bb76a> in <module>()
    ----> 1 xeger.Xeger().xeger(re)
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in xeger(self, string_or_regex)
         80 
         81         parsed = re.sre_parse.parse(pattern)
    ---> 82         result = self._build_string(parsed)
         83         self._cache.clear()
         84         return result
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _build_string(self, parsed)
         87         newstr = []
         88         for state in parsed:
    ---> 89             newstr.append(self._handle_state(state))
         90         return ''.join(newstr)
         91 
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_state(self, state)
         92     def _handle_state(self, state):
         93         opcode, value = state
    ---> 94         return self._cases[str(opcode).lower()](value)
         95 
         96     def _handle_group(self, value):
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <lambda>(x)
         69             "groupref": lambda x: self._cache[x],
         70             'min_repeat': lambda x: self._handle_repeat(*x),
    ---> 71             'max_repeat': lambda x: self._handle_repeat(*x),
         72             'negate': lambda x: [False],
         73         }
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_repeat(self, start_range, end_range, value)
        113         times = randint(start_range, end_range)
        114         for i in xrange(times):
    --> 115             result.append(''.join(self._handle_state(i) for i in value))
        116         return ''.join(result)
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <genexpr>(.0)
        113         times = randint(start_range, end_range)
        114         for i in xrange(times):
    --> 115             result.append(''.join(self._handle_state(i) for i in value))
        116         return ''.join(result)
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_state(self, state)
         92     def _handle_state(self, state):
         93         opcode, value = state
    ---> 94         return self._cases[str(opcode).lower()](value)
         95 
         96     def _handle_group(self, value):
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <lambda>(x)
         64             'branch':
         65                 lambda x: ''.join(self._handle_state(i) for i in choice(x[1])),
    ---> 66             "subpattern": lambda x: self._handle_group(x),
         67             "assert": lambda x: ''.join(self._handle_state(i) for i in x[1]),
         68             "assert_not": lambda x: '',
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_group(self, value)
         95 
         96     def _handle_group(self, value):
    ---> 97         result = ''.join(self._handle_state(i) for i in value[1])
         98         if value[0]:
         99             self._cache[value[0]] = result
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <genexpr>(.0)
         95 
         96     def _handle_group(self, value):
    ---> 97         result = ''.join(self._handle_state(i) for i in value[1])
         98         if value[0]:
         99             self._cache[value[0]] = result
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_state(self, state)
         92     def _handle_state(self, state):
         93         opcode, value = state
    ---> 94         return self._cases[str(opcode).lower()](value)
         95 
         96     def _handle_group(self, value):
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <lambda>(x)
         58                 lambda x: choice(string.printable.replace(unichr(x), '')),
         59             "at": lambda x: '',
    ---> 60             "in": lambda x: self._handle_in(x),
         61             "any": lambda x: choice(string.printable.replace('\n', '')),
         62             "range": lambda x: [unichr(i) for i in xrange(x[0], x[1] + 1)],
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_in(self, value)
        101 
        102     def _handle_in(self, value):
    --> 103         candidates = list(itertools.chain(*(self._handle_state(i) for i in value)))
        104         if candidates[0] is False:
        105             candidates = set(string.printable).difference(candidates[1:])
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <genexpr>(.0)
        101 
        102     def _handle_in(self, value):
    --> 103         candidates = list(itertools.chain(*(self._handle_state(i) for i in value)))
        104         if candidates[0] is False:
        105             candidates = set(string.printable).difference(candidates[1:])
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in _handle_state(self, state)
         92     def _handle_state(self, state):
         93         opcode, value = state
    ---> 94         return self._cases[str(opcode).lower()](value)
         95 
         96     def _handle_group(self, value):
    
    ~/virtualenvs/jupyter_scipy_pandas/lib/python3.5/site-packages/xeger/xeger.py in <lambda>(x)
         61             "any": lambda x: choice(string.printable.replace('\n', '')),
         62             "range": lambda x: [unichr(i) for i in xrange(x[0], x[1] + 1)],
    ---> 63             "category": lambda x: self._categories[x](),
         64             'branch':
         65                 lambda x: ''.join(self._handle_state(i) for i in choice(x[1])),
    
    KeyError: CATEGORY_DIGIT
    
    opened by horvatha 5
Owner
Colm O'Connor
Colm O'Connor
A library for pattern matching on symbolic expressions in Python.

MatchPy is a library for pattern matching on symbolic expressions in Python. Work in progress Installation MatchPy is available via PyPI, and

High-Performance and Automatic Computing 151 Dec 24, 2022
This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

This is a Python 3.10 port of mock, a library for manipulating human-readable message strings.

Alexander Bartolomey 1 Dec 31, 2021
Python library to decorate and beautify strings

outputformater Python library to decorate and beautify your standard output ?? I

Felipe Delestro Matos 259 Dec 13, 2022
From "fixed RAnDom CRashes" to "[FIX] Fixed random crashes."

Clean Commit From fixed RAnDom CRashes to [FIX] Fixed random crashes. Clean commit helps you by auto-formating your commits to make your repos better

Mathias 3 Dec 26, 2021
Waydroid is a container-based approach to boot a full Android system on a regular GNU/Linux system like Ubuntu.

Waydroid is a container-based approach to boot a full Android system on a regular GNU/Linux system like Ubuntu.

WayDroid 4.7k Jan 8, 2023
poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions

poetry2nix poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions. It does so by parsing pyproject.t

Nix community projects 405 Dec 29, 2022
Analisador de strings feito em Python // String parser made in Python

Este é um analisador feito em Python, neste programa, estou estudando funções e a sua junção com "if's" e dados colocados pelo usuário. Neste código,

Dev Nasser 1 Nov 3, 2021
A way to write regex with objects instead of strings.

Py Idiomatic Regex (AKA iregex) Documentation Available Here An easier way to write regex in Python using OOP instead of strings. Makes the code much

Ryan Peach 18 Nov 15, 2021
A Bot Which Can generate Random Account Based On Your Hits.

AccountGenBot This Bot Can Generate Account With Hits You Save (Randomly) Keyfeatures Join To Use Support Limit Account Generation Using Sql Customiza

DevsExpo 30 Oct 21, 2022
A program to generate random numbers b/w 0 to 10 using time

random-num-using-time A program to generate random numbers b/w 0 to 10 using time it uses python's in-built module datetime and an equation which retu

Atul Kushwaha 1 Oct 1, 2022
JLC2KICAD_lib is a python script that generate a component library for KiCad from the JLCPCB/easyEDA library.

JLC2KiCad_lib is a python script that generate a component library (schematic, footprint and 3D model) for KiCad from the JLCPCB/easyEDA library. This script requires Python 3.6 or higher.

Nicolas Toussaint 73 Dec 26, 2022
Ikaros is a free financial library built in pure python that can be used to get information for single stocks, generate signals and build prortfolios

Ikaros is a free financial library built in pure python that can be used to get information for single stocks, generate signals and build prortfolios

Salma Saidane 64 Sep 28, 2022
Random Turkish name generator with realistic probabilities.

trnames Random Turkish name generator with realistic probabilities. Based on Trey Hunner's names package. Installation The package can be installed us

Kaan Öztürk 20 Jan 2, 2023
Simple calculator with random number button and dark gray theme created with PyQt6

Calculator Application Simple calculator with random number button and dark gray theme created with : PyQt6 Python 3.9.7 you can download the dark gra

Flamingo 2 Mar 7, 2022
Rates how pog a word or user is. Not random and does have *some* kind of algorithm to it.

PogRater :D Rates how pogchamp a word is :D A fun project coded by JBYT27 using Python3 Have you ever wondered how pog a word is? Well, congrats, you

an aspirin 2 Jun 25, 2022
A random cat fact python module

A random cat fact python module

Fayas Noushad 4 Nov 28, 2021
Draw random mazes in python

a-maze Draw random mazes in python This program generates and draws a rectangular maze, with an entrance on one side and one on the opposite side. The

Andrea Pasquali 1 Nov 21, 2021
Blender pluggin (python script) that adds a randomly generated tree with random branches and bend orientations

Blender pluggin (python script) that adds a randomly generated tree with random branches and bend orientations

Travis Gruber 2 Dec 24, 2021
Python program that generates random user from API

RandomUserPy Author kirito sate #modules used requests, json, tkinter, PIL, urllib, io, install requests and PIL modules from pypi pip install Pillow

kiritosate 1 Jan 5, 2022