Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:33+00:00 2026-05-25T00:56:33+00:00

I have a regular expression that was the output of a computer program. It

  • 0

I have a regular expression that was the output of a computer program. It has things like

(((2)|(9)))*

which a human would undoubtedly write as

[29]*

So I’d like a program that can make simple transformations that make the regular expression more readable. So far I’ve been using a quick script

$r =~ s/\(([0-9])\)/$1/g;
$r =~ s/\(([0-9])\|([0-9])\)/[$1$2]/g;
$r =~ s/\(([0-9]|\[[0-9]+\])\)\*/$1*/g;
$r =~ s/\((\[[0-9]+\]\*)\)/$1/g;
$r =~ s/\|\(([^()]+)\)\|/|$1|/g;

that bring down the length, but the result still contains pieces like

(ba*b)|ba*c|ca*b|ca*c

that should be simplified to

[bc]a*[bc]

I searched CPAN and found Regexp::List, Regexp::Assemble, and Regexp::Optimizer. The first two don’t apply and the third has issues. First, it won’t pass its tests so I can’t use it unless I force install Regexp::Optimizer in cpan. Second, even once I do that it chokes on the expression.


Note: I tagged this [regular-language] in addition to [regex] because the regexp uses only concatenation, alternation, and the Kleene star, so it is in fact regular.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:56:34+00:00Added an answer on May 25, 2026 at 12:56 am

    I feel like there might be a way to do this by converting the regular expression into a grammar, putting the grammar into Chomsky Normal Form, merging common nonterminals, and looking for patterns using some comparison heurstic. You might even get more concise answers if you don’t put it in “real” CNF… I’d leave the lambdas/epsilons inside.

      ba*b|ba*c|ca*b|ca*c
    
      S -> bAb | bAc | cAb | cAc
      A -> aA | lambda
    
      S -> BAB | BAC | CAB | CAC
      A -> AA | a | lambda
      B -> b
      C -> c
    
      S -> DB | DC | EB | EC
      A -> AA | a | lambda
      B -> b
      C -> c
      D -> BA
      E -> CA
    

    At this point, maybe you find a heuristic that recognizes

      S -> (D+E)(B+C)
    

    Backsubstituting,

      S -> (BA|CA)(b|c) -> (ba*|ca*)(b|c)
    

    Repeat this on subexpressions, e.g.,

      S' -> bA' | cA'
      A' -> aA' | lambda
    
      S' -> B'A' | C'A'
      A' -> A'A' | a | lambda
      B' -> b
      C' -> c
    

    Now recognizing that S -> (B|C)(A), we can get

     S' -> (B'|C')(A') -> (b|c)(a*)
    

    For a final solution of

     S -> ((b|c)a*)(b|c)
    

    Then, you can just look for excess parentheses to remove (noting that concatenation is associative, and this will essentially optimize things into concatenative normal form, just drop all parentheses that don’t enclose only a |-delimited list of options… so the above becomes

      (b|c)a*(b|c)
    

    The trick is coming up with the heuristic, and this may not do all the optimizations which are possible. I don’t know how it will perform. Still, it might be something to consider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a regular expression that can work in a simple
I have a Python regular expression that contains a group which can occur zero
Does anyone have a regular expression handy that will match any legal DNS hostname
Over the years I have slowly developed a regular expression that validates most email
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
I have a regular expression that works fine for me when used in ASP.NET
I have a regular expression that I use to grab data between two sets
I'm trying to write a regular expression that matches the (non-javadoc) comments in the
So I am trying to write a regular expression that will match a defined
i'm getting nuts with an regular expression. I have a string like this: %closed%

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.