13

In Python language I find rstr that can generate a string for a regex pattern.

Or in Python we have this method that can return range of string:

re.sre_parse.parse(pattern)
#..... ('range', (97, 122)) ....

But In Ruby I didn't find any thing.

So how to generate string for a regex pattern in Ruby(reverse regex)?

I wanna to some thing like this:

"/[a-z0-9]+/".example
#tvvd
"/[a-z0-9]+/".example
#yt
"/[a-z0-9]+/".example
#bgdf6
"/[a-z0-9]+/".example
#564fb

"/[a-z0-9]+/" is my input. The outputs must be correct string that available in my regex pattern. Here outputs were: tvvd , yt , bgdf6 , 564fb that "example" method generated them. I need that method.

Thanks for your advice.

1

5 Answers 5

11

You can also use the Faker gem https://github.com/stympy/faker and then use this call:

 Faker::Base.regexify(/[a-z0-9]{10}/)
Sign up to request clarification or add additional context in comments.

Comments

9

In Ruby:

/qweqwe/.to_s
# => "(?-mix:qweqwe)"

When you declare a Regexp, you've got the Regexp class object, to convert it to String class object, you may use Regexp's method #to_s. During conversion the special fields will be expanded, as you may see in the example., using:

(using the (?opts:source) notation. This string can be fed back in to Regexp::new to a regular expression with the same semantics as the original.

Also, you can use Regexp's method #inspect, which:

produces a generally more readable version of rxp.

/ab+c/ix.inspect        #=> "/ab+c/ix"

Note: that the above methods are only use for plain conversion Regexp into String, and in order to match or select set of string onto an other one, we use other methods. For example, if you have a sourse array (or string, which you wish to split with #split method), you can grep it, and get result array:

array = "test,ab,yr,OO".split( ',' )
# => ['test', 'ab', 'yr', 'OO']

array = array.grep /[a-z]/
 # => ["test", "ab", "yr"]

And then convert the array into string as:

array.join(',')
# => "test,ab,yr"

Or just use #scan method, with slightly changed regexp:

"test,ab,yr,OO".scan( /[a-z]+/ )
# => ["test", "ab", "yr"] 

However, if you really need a random string matched the regexp, you have to write your own method, please refer to the post, or use ruby-string-random library. The library:

generates a random string based on Regexp syntax or Patterns.

And the code will be like to the following:

pattern = '[aw-zX][123]'
result = StringRandom.random_regex(pattern)

9 Comments

Can you explain me how to use it?
thanks for your replay , can you take an example how to generate string? means when I tell /[a-z]/.... => it shows me for example "test,ab,yr,..."
@MortezaIpo Generally no, because you dont have source for selection, it just convert the regexp into string, but if you have a sourse array, you can grep it, and get result array: ['test', 'ab', 'yr', 'OO'].grep /[a-z]/ # => ["test", "ab", "yr"] and then convert the array into string as: array.join(',')
@MortezaIpo if my naswer is that you didn't wish, please update your questions woth a proper examples: what you have, and what do you wish in a result.
@MortezaIpo I really don't understand, which is your input condition. For example, I have the string, I should apply something rule to get other string. I see the rule, and I see the output, but don't see the input.
|
8

A bit late to the party, but - originally inspired by this stackoverflow thread - I have created a powerful ruby gem which solves the original problem:

https://github.com/tom-lord/regexp-examples

/this|is|awesome/.examples #=> ['this', 'is', 'awesome']
/https?:\/\/(www\.)?github\.com/.examples #=> ['http://github.com', 'http://www.github.com', 'https://github.com', 'https://www.github.com']

Comments

1

UPDATE: Now regular expressions supported in string_pattern gem and it is 30 times faster than other gems

require 'string_pattern'
/[a-z0-9]+/.generate

To see a comparison of speed https://repl.it/@tcblues/Comparison-generating-random-string-from-regular-expression


I created a simple way to generate strings using a pattern without the mess of regular expressions, take a look at the string_pattern gem project: https://github.com/MarioRuiz/string_pattern

To install it: gem install string_pattern

This is an example of use:

# four characters. optional: capitals and numbers, required: lower
"4:XN/x/".gen    # aaaa, FF9b, j4em, asdf, ADFt

Comments

-1

Maybe you can find what you are looking for over here.

1 Comment

Inspired largely by this bit of code, I have created a gem which does everything discussed there (and much more): github.com/tom-lord/regexp-examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.