0

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?

1
  • Do you want to add one more space if there is already a space before digit? Commented Jun 23, 2014 at 20:19

3 Answers 3

3
sample = sample.replaceAll("\\d+"," $0");
Sign up to request clarification or add additional context in comments.

Comments

3

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.

7 Comments

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
You need to add regex group for that too
@EdwardM.B. I am in the middle of editing my answer to include this :)
@Pshemo to use reference to stored values in regex it's necessary to use groups. Should your regex be "(\\d+)" ?
@Fede $0 is reference to group 0 which represents match from entire regex. I don't need any additional groups here.
|
1

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);
}
}

2 Comments

There can be any digit.
I think the whole point of using regex was so the OP wouldn't have to have something separate for each number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.