在Ruby中为Regex模式生成字符串 [英] Generate string for Regex pattern in Ruby

查看:89
本文介绍了在Ruby中为Regex模式生成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python语言中,我发现 rstr 可以为

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

或者在Python中,我们有此方法可以返回字符串范围:

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

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

但是在Ruby中,我什么都没找到.

But In Ruby I didn't find any thing.

那么如何在Ruby(反向正则表达式)中为正则表达式模式生成字符串?

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

我想要这样的事情:

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

"/[a-z0-9] +/" 是我的输入. 输出必须是我的regex模式中可用的正确字符串. 这里的输出是: tvvd,yt,bgdf6,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.

感谢您的建议.

推荐答案

在Ruby中:

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

在声明Regexp时,您具有Regexp类对象,要将其转换为String类对象,可以使用Regexp的方法

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:

(使用(?opts:source)表示法.此字符串可以反馈到Regexp :: new到具有与原始语义相同的正则表达式.

(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.

此外,您可以使用Regexp的方法

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

生成通常更具可读性的rxp版本.

produces a generally more readable version of rxp.

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

注意:以上方法仅用于将Regexp普通转换为String,并且为了将字符串集匹配或选择到另一个字符串上,我们使用其他方法.例如,如果您有一个sourse数组(或希望通过#split方法拆分的字符串),则可以对其进行grep并获取结果数组:

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"

或仅使用#scan方法,并对其正则表达式稍作更改:

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

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

但是,如果确实需要一个与正则表达式匹配的随机字符串,则必须编写自己的方法,请参考 ruby​​-string-random 库.库:

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:

根据Regexp语法或模式生成随机字符串.

generates a random string based on Regexp syntax or Patterns.

代码如下:

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

这篇关于在Ruby中为Regex模式生成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆