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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:30:33+00:00 2026-05-27T06:30:33+00:00

I will like to implement a simple Wiki-like mark up parser as a exercise

  • 0

I will like to implement a simple Wiki-like mark up parser as a exercise of using Scala parser combinator.

I would like to solve this bit by bit, so here is what I would like to achieve in the first version: a simple inline literal markup.

For example, if the input string is:

This is a sytax test ``code here`` . Hello ``World``

The output string should be:

This is a sytax test <code>code here</code> . Hello <code>World</code>

I try to solve this by using RegexParsers, and here is what I’ve done now:

import scala.util.parsing.combinator._
import scala.util.parsing.input._

object TestParser extends RegexParsers
{   
    override val skipWhitespace = false

    def toHTML(s: String) = "<code>" + s.drop(2).dropRight(2) + "</code>"

    val words = """(.)""".r
    val literal = """\B``(.)*``\B""".r ^^ toHTML

    val markup = (literal | words)*

    def run(s: String) = parseAll(markup, s) match {
        case Success(xs, next) => xs.mkString
        case _ => "fail"
    }
}

println (TestParser.run("This is a sytax test ``code here`` . Hello ``World``"))

In this code, a simpler input which only contains one <code> markup works fine, for example:

This is a sytax test ``code here``.

become

This is a sytax test <code>code here</code>.

But when I run it with above example, it will yield

This is a sytax test <code>code here`` . Hello ``World</code>

I think this is because the regex I use:

"""\B``(.)*``\B""".r

allowed any characters in `` pairs.

I would like to know know should I limit there could not have nested `` and fix this problem?

  • 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-27T06:30:34+00:00Added an answer on May 27, 2026 at 6:30 am

    Here’s some docs on non-greedy matching:

    http://www.exampledepot.com/egs/java.util.regex/Greedy.html

    Basically it’s starting at the first “ and going as far as it can to get a match, which matches the “ at the end of world.

    By putting a ? after your *, you tell it to do the shortest match possible, instead of the longest match.

    Another option is to use [^`]* (anything EXCEPT `), and that will force it to stop earlier.

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

Sidebar

Related Questions

I'm building a simple RPG using Pygame and would like to implement a drag-and-drop
I would like to implement a simple substitution cipher to mask private ids in
I would like to implement a simple search. Let's say the user enters 'york',
I'm trying to implement a simple Java like language parser in sablecc, although I'm
I'd like to implement a ListView that will have a list of SelectedItems as
How can i make an extension method that will work like this public static
Will a query like this on a recordset work rs.open select * from table
I am trying to implement a simple Observer pattern using .net Observable class. I
I would like to implement an invitation system for an ASP.NET application. The system
I'd like to implement a simple round robin over a Python list or set

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.