regular expression question

java, regex, string

Solution

string.replaceFirst("\\?", yourWord)

That will replace the first occurrence of a `'?'` in your code with whatever `yourWord` is.

If you want to replace every `'?'` with `yourWord` then use `string.replaceAll("\\?", yourWord)`.

See the javadocs for more info.

Problem

I want to replace the question mark (?) in a string with some other words. What is the regular expression for question mark. For example, I want to replace question mark in "word=?" to something else, say "stackoverflow". Then the result would be "word=stackoverflow". What is the syntax in java?

Original source