I have a string like:
"This is my abc.def ght.123 example 12.34 test"
I’d like to change to:
"This is my \"abc def\" \"ght 123\" example 12.34 test"
How to do using regular expression in java?
Thanks
Isa
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If there are only one-dot sequences like
abc.deforght.123try the following:The expression used is means: any sequence of characters followed by a dot and a sequence of characters or digits (the
\bis a word boundary to prevent matches of (_abc.def, if you want them as well you could add the underscore to the character class or remove the\b).The replacement would then replace the match with
\"$1 $2\"where$1is the content of the first group([a-zA-Z]+)and$2is the content of the second group([a-zA-Z0-9]+).Update:
For the required input
"This is my complexe string 12.34 ab.cd a+-.12 34.0+b zer.123.456 or 12.34.56 etc"(see the comment), the following expression might be used:Here
(?i)means the expression should be case insensitive,(?<=^|\s)means match either if we’re at the start of the string or after a whitespace,(?=\s|$)means match if a whitespace or the end of the string follows.The core expression is still
([a-z\+\-]+)\.([a-z0-9\+\-]+). As you can see, I only added+and-to the character classes (note that it would be possible to write-instead of\-but then the meaning of the minus character would depend on its position in the character class). If you want to support more characters, add them to the character classes as needed. For more information on predefined character classes like\dorp{L}see here and here.