1

I need to split a string based on a pattern and again i need to merge it back on a portion of string.

for ex: Below is the actual and expected strings.

String actualstr="abc.def.ghi.jkl.mno";
String expectedstr="abc.mno";

When i use below, i can store in a Array and iterate over to get it back. Is there anyway it can be done simple and efficient than below.

String[] splited = actualstr.split("[\\.\\.\\.\\.\\.\\s]+");

Though i can acess the string based on index, is there any other way to do this easily. Please advise.

Karthik
  • 145
  • 3
  • 15

4 Answers4

2

You do not understand how regexes work.

Here is your regex without the escapes: [\.\.\.\.\.\s]+

You have a character class ([]). Which means there is no reason to have more than one . in it. You also don't need to escape .s in a char class.

Here is an equivalent regex to your regex: [.\s]+. As a Java String that's: "[.\\s]+".


You can do .split("regex") on your string to get an array. It's very simple to get a solution from that point.

Laurel
  • 5,965
  • 14
  • 31
  • 57
1

I would use a replaceAll in this case

String actualstr="abc.def.ghi.jkl.mno";
String str = actualstr.replaceAll("\\..*\\.", ".");

This will replace everything with the first and last . with a .

You could also use split

String[] parts = actualString.split("\\.");
string str = parts[0]+"."+parts[parts.length-1]; // first and last word
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1
public static String merge(String string, String delimiter, int... partnumbers)
{
    String[] parts = string.split(delimiter);

    String result = "";

    for ( int x = 0 ; x < partnumbers.length ; x ++ )
    {
        result += result.length() > 0 ? delimiter.replaceAll("\\\\","") : "";
        result += parts[partnumbers[x]];
    }

    return result;
}

and then use it like:

merge("abc.def.ghi.jkl.mno", "\\.", 0, 4);
1

I would do it this way

    Pattern pattern = Pattern.compile("(\\w*\\.).*\\.(\\w*)");
    Matcher matcher = pattern.matcher("abc.def.ghi.jkl.mno");
    if (matcher.matches()) {
        System.out.println(matcher.group(1) + matcher.group(2));
    }

If you can cache the result of

Pattern.compile("(\\w*\\.).*\\.(\\w*)") 

and reuse "pattern" all over again this code will be very efficient as pattern compilation is the most expensive. java.lang.String.split() method that other answers suggest uses same Pattern.compile() internally if the pattern length is greater then 1. Meaning that it will do this expensive operation of Pattern compilation on each invocation of the method. See java.util.regex - importance of Pattern.compile()?. So it is much better to have the Pattern compiled and cached and reused.

matcher.group(1) refers to the first group of () which is "(\w*\.)" matcher.group(2) refers to the second one which is "(\w*)"

even though we don't use it here but just to note that group(0) is the match for the whole regex.

Community
  • 1
  • 1
klor
  • 3,596
  • 2
  • 13
  • 13