Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find markdown image syntax in String in Java

I've a long text in Java, which contains at least one markdown image syntax. If there're N markdown image syntax, I will need to split the string into N+1 substrings and store them in an array of String, call texts. For example, I've the following text

Hello world!
![Alt text](/1/2/3.jpg)
Hello Stack Overflow!

Then Hello world!\n will be stored in position 0 and \nHello Stack Overflow! will be stored in position 1. For my question, we can assume that

  • The Alt text part contains only character A-Z, a-z and blank space.
  • The URL part contains only digits 0-9 and slash /. Its extension will only be .jpg. Other extension will not exist.

My question is how to split the text ? Do we need a java regular expression, such as *![*](*.jpg) ?

like image 957
Mincong Huang Avatar asked Apr 07 '26 17:04

Mincong Huang


1 Answers

Try this (ready to copy-paste):

"!\\[[^\\]]+\\]\\([^)]+\\)"

See here for info about how to get the matches.

"Untainted" version: !\[[^\]]+\]\([^)]+\)

Explanation

  • ! literally !
  • \[ escaped [
  • [^\]]+ as many not ]s as possible
  • \]\( escaped ](
  • [^)]+ as many not )s as possible
  • \) escaped )
like image 158
Laurel Avatar answered Apr 10 '26 07:04

Laurel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!