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 8799757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:24:32+00:00 2026-06-14T00:24:32+00:00

Sorry for the confusing title but I’m not sure how better to explain it.

  • 0

Sorry for the confusing title but I’m not sure how better to explain it.

I am building a simple web server for a school project that has to parse a custom scripting language. I have a line that looks like this:

<p>Here's the date: <% pr date() %></p><p>Here's the date again: <% pr date() %></p>

I’m using the following regular expression to try and pull out the <% … %> stuff…

<% *(.*) *%>

The problem is it is matching from the first open tag to the last closing tag, rather than from the first open tag to the first closing tag. So the resulting match is this:

<% pr date() %></p><p>Here's the date again: <% pr date() %>

…instead of:

<% pr date() %>

I thought I could solve it by using something like this, but it doesn’t seem to work:

<% *([^(<%)]*) *%>

…but it doesn’t seem to work. Any help is appreciated, thanks.

  • 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-14T00:24:33+00:00Added an answer on June 14, 2026 at 12:24 am

    The problem is it is matching from the first open tag to the last closing tag

    You need a non-greedy match which stops upon the first time a match is recognized:

    .*  --> greedy ("maximum munch")
    .*? --> non-greedy ("minimal munch")
    

    The non-greedy quantifier can of course be applied to most other patterns.

    However, I would advice against using regexes. The meta-pattern OPEN-TOKEN CONTENT CLOSE-TOKEN is simple enough for a hand written parser/scanner. It will then also be easier for you to recognize when your tags are within comments (and possibly other cases were you don’t want a match):

    <!-- <% xyz %> -->
    

    Code like above might not be encouraged by you, but you have to consider that.


    Footnote: Each time you (write a parser|fire a regular expression), you are in prison with one leg already.

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

Sidebar

Related Questions

Sorry for the confusing title, but I'm not sure what to call those. I'm
Sorry for the confusing title, but I couldn't find a better way to explain
sorry if the title is very confusing, but I'm not sure how to really
Sorry if the question title is confusing. Let me explain further. I am building
Sorry for the confusing title :P I'm sure I should know this but I
Sorry for the somewhat confusing title. Not sure really how to title this. My
I am sorry for the confusing title, I'm not sure how to put into
Sorry for the confusing title, I couldnt explain it better. Feel free to edit
Sorry for the confusing title, I'm not sure how to describe it in minimal
Sorry if the title was confusing, i could not think of something better. Basically,

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.