简体   繁体   中英

How can I create a regex that will match several similar strings?

I'm looking for a function that can take multiple strings as an input, and create a regex that will match all of them.

For example, let's say the inputs are:

  • fun dog
  • funny dog
  • fun dogs

It should output something like:

/fun(ny)? dogs?/

I was wondering if Levenshtein distance could be used for such purpose, but no clue... what do you think?

You're not saying which flavor of regex. Perl comes with a thing called Regex::PreSuf which does this sort of thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM