简体   繁体   中英

How to implement a strictly matching markdown with regex in java?

I am trying to make a simple markdown parser in java, I only need one to make string bold and another one to change color to blue and also be able to combine both markdown. So for bold the words need to be places between double stars *, however, i don't want it to match if there are just 5 stars or more with nothing in between (many sites still match that and change 5 stars to a single bold one) so if the user was using the stars to draw a separator line or something they should just be displayed as is, but it should match if there is something with stars or other symbols like a math formula 4*5 (in other words, what is in between should not just be stars)

Here is my code so far, it works to match bold, blue and both but still matches 5+ stars.

str = str.toString().replaceAll("\*\*(.+?)\*\*", "<big>$1</big>");
str = str.toString().replaceAll("\*b(.+?)\*\*", "<font color='blue'>$1</font>");

i tried changing (.+?) to (.+?&&[^\\*]) , (.*?[^\\*].*?) , (.+?[^\\*].*?) and many other but they work for some and fail for other cases!

Examples that should NOT match:

****
*****    (5 and more stars)
*Hi*
***Hi***
**New\nLine**

Examples that should match either or both cases (Bold and Blue):

**Hi** : BOLD
*bHi** : Blue
***bHi**** : BOLD and Blue
*b**Hi**** : BOLD and Blue

also it should not match between multiple lines.

Is what I am trying to do too complicated? can it be done with just regex?

  • This is being done for Android in case it differs from normal java.

I think these two should do it for you:

  • Bold: (^|\\s)\\*{2}([\\w\\d ]+)\\*{2}($|\\s)
  • Blue: (^|\\s)\\*b([\\w\\d ]+)\\*{2}($|\\s)

And I finally decided that the easiest thing to do for testing the combos of blue and bold was to add a new regexp:

  • Blue & Bold: (^|\\s)(\\*{3}b|\\*b\\*{2})([\\w\\d ]+)\\*{4}($|\\s)

This won't be very scalable if you are going to add italic and underline and more colours but I think it will work the best if these two are your only options.

Let me know if there is a case where these doesn't match or if they match when they're not supposed to.

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