Pregunta

I java, I need to replace a double asterisk, only the first occurence. How? I want that:

the first "**" --> "<u>" and the second "**" --> "<\u>"

Example:

String a = "John **Doe** is a bad boy"

should become:

String a = "John <u>Doe<\u> is a bad boy"

using somethig as:

a = a.replaceFirst("**","<u>").replaceFirst("**","<\u>")

How?

¿Fue útil?

Solución

You need to escape the asterisks to avoid them being interpreted as part of a regular expression:

a = a.replaceFirst(Pattern.escape("**"), "<u>");

Or:

a = a.replaceFirst("\\Q**\\E", "<u>")

Or:

a = a.replaceFirst("\\*\\*"), "<u>");

To perform your translation you could do this:

a = a.replaceAll("\\*\\*(.*?)\\*\\*", "<u>$1</u>");

The advantage of a single replaceAll over a pair of replaceFirst calls is that replaceAll would work for strings containing multiple asterisked words, e.g. "John **Doe** is a **bad** boy".

Essentially the matching expression means:

\\*\\*  -- literal "**"
(       -- start a capturing group
.       -- match any character (except LF, CR)
*       -- zero or more of them
?       -- not greedily (i.e. find the shortest match possible)
)       -- end the group
\\*\\*  -- literal "**"

The replacement:

<u>     -- literal <u>
$1      -- the contents of the captured group (i.e. text inside the asterisks)
</u>    -- literal </u>

By the way, I've changed your end tag to </u> instead of <\u> :-)

Depending on your requirements, you might be able to use a Markdown parser, e.g. Txtmark and save yourself reinventing the wheel.

Otros consejos

You can use:

String a = "John **Doe** is a bad boy"
a = a.replaceFirst("\\Q**\\E", "<u>").replaceFirst("\\Q**\\E", "</u>");
//=> John <u>Doe</u> is a bad boy

As mentioned above by aetheria and going with what you already are trying:

a = a.replaceFirst("\\*\\*", "<u>").replaceFirst("\\*\\*", "<\u>");

When you want to try something else, I recommend using the online regex tester below which will show the results of different patterns using replaceFirst, replaceAll, etc on different input strings. It will also provide in the top left the correctly escaped string that should be used in your Java code.

http://www.regexplanet.com/advanced/java/index.html

I would do this:

String a = "John **Doe** is a bad boy";
String b = a.replaceAll("\\*\\*(.*?)\\*\\*", "<u>$1</u>");
//John <u>Doe</u> is a bad boy

LIVE DEMO

REGEX EXPLANATION

\*\*(.*?)\*\*

Match the character “*” literally «\*»
Match the character “*” literally «\*»
Match the regex below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is NOT a line break character (line feed, carriage return, next line, line separator, paragraph separator) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “*” literally «\*»
Match the character “*” literally «\*»

<u>$1</u>

Insert the character string “<u>” literally «<u>»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character string “</u>” literally «</u>»
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top