-1

I really need a regex expert: I need a regex expression (in java) for splitting this examples:

Hello/World (word/word) => Hello,World

Hello/12 (word/number) => Hello,12

15/Hello (number/word) => 15,Hello

12/17 (number/number) => 12/17 (Do not split)

Update:

This is what I tried but it also mark the number/number option https://regex101.com/r/zZ9nO5/2

Thanks

  • 1
    So you don't want to split on `/` if it has digit before and after it. You can use https://www.regular-expressions.info/lookaround.html mechanism here. – Pshemo Nov 14 '18 at 09:48
  • In addition to Pshemo's comment: could there be any whitespace before or after the slash, e.g. `12 / 17`? If so, should that change how you'd split? If not, then you'd need to add that to your regex. Try something with (negative) lookaround and if needed whitespace matching (`\s`) and if you have problems with that show us what you've tried. – Thomas Nov 14 '18 at 09:52
  • there is no spaces after and before. I am really weak at regex, if someone know the exact expression to try it will be amazing. – UnknownUser Nov 14 '18 at 10:07

3 Answers3

0

It might not be the most elegant solution but for your requirement you can do it like that:

(([a-zA-Z]+?)/([a-zA-Z]+))|(([a-zA-Z]+?)/([\d]+))|(([\d]+?)/([a-zA-Z]+))

It's a check for word / word, word / number and number / word

replace with the corresponding groups found \2\5\8,\3\6\9

A simple java program for that would be:

  public static void main(String[] args) {
            String[] stringArray=new String[]{"Hello/World","Hello/12","15/Hello","12/17"}; 
            for(String s:stringArray) {
                System.out.println(s.replaceAll("(([a-zA-Z]+?)/([a-zA-Z]+))|(([a-zA-Z]+?)/([\\d]+))|(([\\d]+?)/([a-zA-Z]+))", "$2$5$8,$3$6$9"));
            }
        }

Result is:

Hello,World
Hello,12
15,Hello
12/17
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

A little more context would be nice but as I understand it, you get a string with a single '/' in the middle and you either replace the '/' with ',' or you dont if it has numbers on both sides. So i would do something like this:

public class MyClass {
 public static void main(String args[]) {
        String mystring = "12/25";

        if(!mystring.matches("^\\d+\\/\\d+$"))
            mystring = mystring.replace("/", ",");

        System.out.println(mystring);
    }
}

If that is what you wanted to do here, then I belive its less complicated and also quicker than a big regex destinguishing between all 4 cases.

Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46
0

Slightly different approach, but you could check the characters in the String to see that they all are either a number or a forward slash, and then split if necessary:

   public static void main(String[] args) {
    String[] strArray = new String[]{"Hello/World", "Hello/12", "15/Hello", "12/17"};

    for(String str: strArray){
        if(checkIfValid(str)){
            System.out.println(str);
        }
        else{
            System.out.println(str.replace("/", ","));
        }
    }
}

public static boolean checkIfValid(String str) {
    for (int i = 0; i < str.length(); i++) {
        if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '/') {
            return false;
        }
    }
    return true;
}

Output:

Hello,World

Hello,12

15,Hello

12/17

This might help if Hello12/15 is not supposed to be split.

Community
  • 1
  • 1
achAmháin
  • 4,176
  • 4
  • 17
  • 40