Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Does anyone know of any kind of regular expression builder that generates regexes from a given string? What I'm looking for is something like this...

Given a string like "Rich36!", it would produce \w{4}\d{2}\! or \w+\d+\!.

The driving force behind this is that I'm working on a tool that uses regular expressions to grab data out of some files. Part of the interface sometimes requires the users to input regular expressions to capture the necessary text. Since most of them are not familiar with regular expressions, I'm looking for a way to allow them to input a string of text and then output a regular expression with metacharacters that would grab text that was like the string they inputted.

I did come across the excellent Regex::PreSuf, which does that, but it doesn't use metacharacters to any great degree.
For instance,

use Regex::PreSuf; my $re = presuf({anychar => 1}, qw(@foo @bar @baz)); print qq($re\n); __RESULT__ \@(?:ba[rz]|foo)

Which is great, but I'm looking for a mechanism that would produce a regex that would capture something like @oof as well (a regex like \@\w{3}).

Any suggestions or information would be greatly appreciated.


«Rich36»

In reply to Regular Expression Builder by Rich36

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2014-10-22 03:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (112 votes), past polls