If you use a library called acceptableRegex.py, you can get a string that matches the regular expression. You can use the results to do something like fuzzing.
Get the file from github.
$ git clone https://github.com/tokoroten/acceptableRegex.git
Place the obtained acceptableRegex.py in a place where PYTHONPATH passes, or Place it in the same directory as the script you are creating.
The usage is in main () of acceptableRegex.py, If you pass a regular expression to getAcceptableRegex, it will return a matching result.
from acceptableRegex import getAcceptableRegex
if __name__ == '__main__':
print getAcceptableRegex('[0-9]{9}')
This sample returns a 9-digit number from 0 to 9.
Using the result of acceptableRegex.py, It seems that it can be used in tests such as programs that read CSV and do something.
sample.py
# -*- coding: utf-8 -*-
import csv
from acceptableRegex import getAcceptableRegex
if __name__ == '__main__':
with open('eggs.csv', 'wb') as f:
csvfile = csv.writer(f)
for x in range(1, 100):
syouhin = getAcceptableRegex('[a-zA-Z]{10}')
price = getAcceptableRegex('[01]{1}[0-9]{3}')
category = getAcceptableRegex('(aaa|bbb|ccc|fff)')
csvfile.writerow([syouhin, price, category])
Recommended Posts