0

I'm trying to do a markdown parser. Basically, right now I just want to transform something like: "this is a *italic* text" into "this is a <em>italic</em> text".

I have basically this:

html_text = html_text.replaceAll("\*(.+)\*", "<em>$1</em>");

Here's the problem. If I use replaceAll, in a string such as "this *is* a *test* ok." it doesn't replace it 2 times, only once. Anyway, it can be fixed using replaceFirst() several times, so it's no biggie, still I don't get why it's replaceAll() and it doesn't replace all...

Anyway, using replaceFirst() I get one replacement, with the result as follows: "this <em>is* a *test</em> ok." I don't know much about regex, but I want it to replace from left to right not use some arbitrary rule. This is, the first block it should find is "*is*" not "*is* a *test*" giving as a result "this <em>is</em> a *test* ok.". Anyway, doing it another time the output is: "this <em>is<em> a </em>test</em> ok." which is wrong. A left to right would give the right one which is: "this <em>is</em> a <em>test</em> ok."

Since I don't know much about regex and I've been looking for a while I've decided to just ask you guys.

TL.DR.: I want replaceFirst() to replace using a left to right order, not some arbitrary one.

Edit: A solution is not allowing the * symbol inside the search. replaceAll() works fine. Still allowing the search from left to right would solve the problem much more easily, so is it possible?

4

1 に答える 1

0

これを試して:

\*([^*]+)\*(アスタリスク以外を 1 つ以上取る)

またはこれ:

\*(.+?)\*(貪欲でない + のバージョンなので、アスタリスクまでの最小の一致が必要です)

あなた(.+)は貪欲なので、最後のアスタリスクに行き、あなたが望む以上のものをキャプチャします. 私の考えでは、貪欲でない正規表現の方がエレガントですが、どちらでも機能します。

于 2013-09-19T18:23:01.893 に答える