Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have strings with values "Address Line1", "Address Line2" ... etc. I want to add a space if there is any numeric value in the string like "Address Line 1", "Address Line 2".

I can do this using contains and replace like this

String sample = "Address Line1";
if (sample.contains("1")) {
    sample = sample.replace("1"," 1");
}

But how can I do this using regex?

share|improve this question
    
Do you want to add one more space if there is already a space before digit? –  Braj Jun 23 at 20:19
add comment

3 Answers

sample = sample.replaceAll("\\d+"," $0");
share|improve this answer
add comment

To use regex you will need replaceAll instead of replace method:

  • as regex you can use

    • \\d+ to match any group of one or more continues digits. We need all continues digits here because matching only one would create from foo123 something like foo 1 2 3
    • (?<=[a-zA-Z])\\d if you want to add space only before digit which has alphabetic character before it. (?<=\\[a-zA-Z]) part is look-behind and it just checks if tested digit has character from range a-z or A-Z before it.
  • and as replacement you can use " $0 which means space and match from group 0 which means part currently matched by regex.

So try with

sample = sample.replaceAll("\\d+", " $0")

or

sample = sample.replaceAll("(?<=[a-zA-Z])\\d", " $0")

which will change "hello 1 world2" into "hello 1 world 2" - notice that only 2 has additional space.

share|improve this answer
    
i agree, but would it be better to check if there is a character before it just in case? and put the space between both groups –  Edward M.B. Jun 23 at 19:58
    
You need to add regex group for that too –  Fede Jun 23 at 19:59
    
@EdwardM.B. I am in the middle of editing my answer to include this :) –  Pshemo Jun 23 at 20:00
    
@Fede Why? It works fine for me. –  Pshemo Jun 23 at 20:00
    
@Pshemo to use reference to stored values in regex it's necessary to use groups. Should your regex be "(\\d+)" ? –  Fede Jun 23 at 20:03
show 4 more comments

First Create a Pattern Object of what you want to search and compile it in your case Pattern object will be as follows:-

Pattern p=Pattern.compile("1");

Now Create Matcher object for your string

Matcher m=p.matcher(sample);

Now put a condition to check if Matcher has found any your Pattern String and if it has put a replaceAll method to replace it

if(m.find())
{   
sample=m.replaceAll(" 1");
}

The Complete code is as follows:-

import java.io.*;
import java.util.regex.*;
class demo
{
public static void main(String args[])
{
    String sample = "Address Line1";
    Pattern p=Pattern.compile("1");
    Matcher m=p.matcher(sample);
    if(m.find())
    {   
            sample=m.replaceAll(" 1");
    }
    System.out.println(sample);
}
}
share|improve this answer
    
There can be any digit. –  Braj Jun 23 at 20:11
    
I think the whole point of using regex was so the OP wouldn't have to have something separate for each number. –  smerny Jun 23 at 20:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.