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

  • SEARCH
  • Home
  • 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 8980839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:11:12+00:00 2026-06-15T20:11:12+00:00

I understand that Prolog programmers typically use DCGs instead of regular expressions for matching

  • 0

I understand that Prolog programmers typically use DCGs instead of regular expressions for matching patterns in a string. In Perl, one might write

if ( '... accd' =~ /a+b*c{2,4}d$/ ) {
    say "matched";
}

How would one match the same pattern in Prolog?

  • 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-06-15T20:11:13+00:00Added an answer on June 15, 2026 at 8:11 pm

    I’ve extended this answer

    :- op(100, xf, *).
    :- op(100, xf, +).
    
    rexp(C) --> [C].
    
    rexp([T|Ts])   --> rexp(T), rexp(Ts).
    rexp([])       --> [].
    
    rexp(eps)      --> [].
    
    rexp(_*)       --> [].
    rexp(R*)       --> rexp(R), rexp(R*).
    
    rexp(R+)       --> rexp(R), rexp(R*).
    
    rexp((R1|R2))  --> ( rexp(R1) ; rexp(R2) ).
    
    rexp(range(R,N,M)) -->
        {between(N,M,L),
         length(D,L),
         maplist(copy_term(R),D)
        }, rexp(D).
    

    then your regular expression match could be

    ?-  phrase(rexp([a+, b*, range(c,2,4), d]), [a,c,c,d]),
        writeln(matched).
    

    Note that in this way we match atoms instead of single characters.

    edit after false’ comment, I think that the first clause should read

    rexp(C) --> {atomic(C)}, [C].
    

    to avoid for instance

    ?- phrase(rexp([a+]), [a+]).
    true ;
    

    Indeed, after the correction we have the expected outcome:

    ?- phrase(rexp([a+]), [a+]).
    false.
    

    done edit

    Instead of interpreting regular expressions the pattern could be ‘hardcoded’ (much easier)

    % I prefer the equivalent clause below
    % p1 --> "a", p1 ; "a", p2.
    p1 --> "a", (p1 ; p2).
    p2 --> "b", p2 ; p3.
    p3 --> ("cc" ; "ccc" ; "cccc"), "d".
    

    then

    ?- phrase(p1, "accd").
    true
    

    here we match single characters (a string in Prolog it’s a list of character codes)

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

Sidebar

Related Questions

I understand that in order to sum array elements in Ruby one can use
What is one good for that the other's not in practice? I understand the
I understand that normally you use copy for NSStrings, so that your property stays
I try to understand how axiom resolution works in prolog. Let's assume that I
As many programmers I studied Prolog in university, but only very little. I understand
I am having a problem with prolog that I do not understand. I have
I understand that fields such as Html.TextBox() accept two values, the first one being
I understand that only one instance of any object according to .equals() is allowed
I understand that in general it is a bad idea to use SELECT *
I understand that if else staments should say stuff like: if dog equals one,

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.