0

My string length is allowed not more than 255 characters. The following Regular expression can be taken for example-

^([a-zA-Z0-9]{1}((-|[a-zA-Z0-9])*[a-zA-Z0-9])*[.])*[a-zA-Z]{1}((-|[a-zA-Z0-9])*[a-zA-Z0-9])*\.?$

i am using exrex.getone(regular_expression) function for generating strings where i can't control generated string length.
How to generate a string of intended length which satisfy the Regular Expression also ?
Till now i have not seen any answer satisfying this condition.

MadhuSudan
  • 23
  • 7
  • 1
    Possible duplicate of [Reversing a regular expression in Python](http://stackoverflow.com/questions/492716/reversing-a-regular-expression-in-python) – ns15 Nov 24 '16 at 07:12
  • @shadow0359 i have already checked the answers here (Reversing a regular expression in Python) but it doesn't satisfy the condition mentioned . So can you please remove it as duplicate.Thanks. – MadhuSudan Nov 24 '16 at 07:27
  • 1
    Does exrex support assertions, so you could prefix the regex like `^(?=.{0,255}$)...`? – Stefan Pochmann Nov 24 '16 at 07:33
  • If you can't find an existing library that can do the length limiting, you can always do it yourself. Build it on top of a full-fledged existing library or something like [this](http://stackoverflow.com/a/5570853/680727). (That was built for code-bowling, but the bowling part is the use of reverse regexes in the first place; the code itself isn't bad - In fact, the actual generating is only 30 lines of quite readable code) – Aleksi Torhamo Nov 24 '16 at 07:42
  • What's wrong with the results you get from your current exrex.getone attempt? – Stefan Pochmann Nov 24 '16 at 07:47
  • @StefanPochmann Hi Stefan ,it is generating strings of random lengths which are exceeding length limits of 255. – MadhuSudan Nov 24 '16 at 07:55
  • @MadhuSudan Always? Or could you just ask it again and again until it gives you one with an allowed length? – Stefan Pochmann Nov 24 '16 at 08:03
  • @StefanPochmann actually i want the expexted result always – MadhuSudan Nov 24 '16 at 08:49
  • @MadhuSudan Huh? Why? What does that matter? – Stefan Pochmann Nov 24 '16 at 11:16

1 Answers1

0

From exrex website:

There are regular expressions with infinite matching strings (eg.: [a-z]+ ), in these cases exrex limits the maximum length of the infinite parts.

and the code itself:

418 def getone(regex_string, limit=20): 
419     """Returns a random matching string to a given regular expression 
420     """ 
421     return _randone(parse(regex_string), limit) 

So there's a limit keyword. I couldn't tell for sure if this limits the entire string or each infinite part, but I think it's the latter. Try limiting this to something smaller, like 4.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • thanks for the great info, But i am really surprised that for the regular expression ^sip:\S+$ exrex is generating string _sip:_ which is wrong. i know this is not related to the current issue but just asking. can you please help here.how can it be possible that the function like exrex() which is recommended for regex can generate invalid string.thanks. – MadhuSudan Nov 24 '16 at 11:24
  • That regular expression you write means: From start of line write "sip:" and then write none-spaces until the end. I checked and exrex may not support the 'not' versions of the common character sets, so '\S' may indeed not work. Use [^\s] instead - worked for me. – kabanus Nov 24 '16 at 11:48
  • you mean **^\sip:\S+$** but this didn't work for me. Can you please share ,which regex you used ? And Can you please share any regex generator where these types of regex are supported ? Actually in my script i am generating string from 100's of regex like that only, so i can not edit them manually. I have to use some string generator from these regex that can work fine in this case. Thanks a lot. – MadhuSudan Nov 24 '16 at 12:53
  • You missed the first \s so: "^\sip:[^\s]*$". \S isn't that important. exrex is complete, just don't use \S and similar expressions. – kabanus Nov 24 '16 at 12:55
  • Can you please share any regex generator where these types of regex are supported ? Actually in my script i am generating string from 100's of regex like that only, so i can not edit them manually. I have to use some string generator from these regex that can work fine in this case. Thanks a lot. – MadhuSudan Nov 25 '16 at 08:13
  • I don't really understand what you're asking. Please accept this answer if it answered your original question, and post a new question with code and examples for what you need now. You can link to it in the comments here and I'll check it out. – kabanus Nov 25 '16 at 09:17