1

I have 2 Scenarios:

  1. String Starts with Sample Country ! i.e. Sample Country ! Test Data

I want a regex to replace Sample Country ! with Empty String, Country here is not fixed, it can be US, France etc

I tried:

System.out.println(str.replaceAll("^(Sample[^!]+!)", ""));

I am getting the Output

! Test Data 

whereas I just want

Test Data
  1. String ends with Sample Country ! i.e. Test Data Sample Country ! here also I just want

    Test Data

Can someone help to provide the correct Regular expression with the explanation. Thanks a lot

user3306543
  • 201
  • 1
  • 3
  • 12
  • The regex in your first scenation returns a space followed by `Test Data` see https://ideone.com/wVLloT. You might update the regex to `^Sample[^!]+!\\s*` as you don't need the capturing group if you are replacing it with an empty string and you match 0+ times a trailing whitespace character as well. But for the second scenario, how would you know the difference between `Test Data` and `Sample Country` if the country name is one or two words? – The fourth bird Jan 18 '19 at 10:16

2 Answers2

0

Edit :

let's make a better way. you will have not only 2 cases you will have 3 cases

  1. (pattern + data) ---> ^Sample[^!]+! (pattern) ([^!]) (data)

  2. (data +pattern) ---> ([^!]) (data) Sample[^!]+!$ (pattern)

  3. (pattern + data + pattern) ---> (^Sample[^!]+! (pattern) ([^!]) (data) Sample[^!]+!$ (pattern)

so we have to check all the cases in our string with regex. we need OR cases in regex it is "|" another thing is we have to avoid not matched cases must be ignored it is with (?:(regex)) descripted here

public class HelloWorld {

public static void main(String[] args) {
    String[] testcases = new String[] {
        "Sample foo ! Test1 Data",
        "Sample bar ! Test2 Data",
        "Test3 Data Sample foo !",
        "Test4 Data Sample bar !",
        "Sample bar ! Test5 Data Sample bar !"
    };

    for (String str: testcases) {
        System.out.println(str.replaceAll("(?:(^Sample[^!]+!([^!])))|(?:(([^!])Sample[^!]+!$))|(?:(^Sample[^!]+!([^!]))Sample[^!]+!$)", "$2$4").trim());
    }

}

} we used your regex and make a new regex after grouping data will be at ($2,$4) groups because of that we replace the string with 2nd and 4th group values. I hope this will help. compile code online

Saltuk
  • 1,159
  • 9
  • 12
  • downvote . why ? "I want a regex to replace Sample Country ! with Empty String, Country here is not fixed, it can be US, France et . " and here is the answer . if you have samples you have the describe whole data example – Saltuk Jan 18 '19 at 00:28
0

Try this regex here:

String[] testcases = new String[] {
    "Sample foo ! Test Data", 
    "Sample bar ! Test Data", 
    "Test Data Sample foo !", 
    "Test Data Sample bar !"
};

for (String str : testcases) {
    System.out.println(str.replaceAll("(.* ?)(Sample[a-zA-Z ]+ ! ?)(.*)", "$1$3"));
}

Explanation:

(.* ?) // first match group, matches anything, followed by an optional space

(Sample[a-zA-Z ]+ ! ?) // second match group, matches the String "Sample followed by a series of letters (your country), a whitespace, an exclamation mark and an optional space

(.*) // third match group, matches anything

So the second match group ($2) will contain your "Sample Country" string and we can just replace the result with only the first ($1) and the third ($3) match group.

Patric
  • 1,489
  • 13
  • 28