开发者

How to provide regular expression for matching $$

I am having String str = "$$\\frac{6}{8}$$"; I want to match for strings using starting with '$$' and ending with '$$'

How开发者_StackOverflow社区 to write the regular expression for this?


Try using the regex:

^\$\$.*\$\$$

which in Java will be:

^\\$\\$.*\\$\\$$

A $ is a regex metacharacter used as end anchor. To mean a literal $ you need to escape it with a backslash \.

In Java \ is the escape character in a String and also in the regular expression. So to make a \ reach the regex engine you need to have \\ in the String.

See it


Use this regex string:

"^$$.*$$$"

The ^ anchors the expression to the start of the string being matched, and the last $ anchors it to the end. All other $ characters are taken literally.


You may want something like this:

final String str = "$$\\frac{6}{8}$$";
final String latex = "A display math formula " + str + " and once again " + str + " and another one " + "$$42.$$";
final Pattern pattern = Pattern.compile("\\$\\$([^$]|\\$[^$])+\\$\\$");
final Matcher m = pattern.matcher(latex);
while (m.find()) {
    System.out.println(m.group());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜