Regex for splitting word / (slash) word

regex split string by character
regex split string python
split string using regex javascript
java regex
split using regex javascript
regex tutorial
java regex tester
regex split pattern

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)


This is what I tried but it also mark the number/number option


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


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:


How to Split String in Java using Regular Expression, takes a regular expression, which in simplest case can be a single word. split() is also is to escape backward slash which is an escape character in Java. Split (String) Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor. Split (String, String) Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

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






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

Regex.Split Method (System.Text.RegularExpressions), Splits an input string into an array of substrings at the positions defined by NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled sets of capturing parentheses to extract the individual words in a string. I know you specifically asked for regex, but you don't really need regex for this. You simply need to split the string by delimiters (in this case a backslash), then choose the part you need (in this case, the 3rd field - the first field is empty).

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";

            mystring = mystring.replace("/", ",");


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.

[PDF] Regular Expressions: The Complete Tutorial, The fact that this “a” is in the middle of the word does not matter to the regex engine. The backslash in combination with a literal character can create a regex token If you want Java to return the same match as Perl, you'll need to split off the  Just thought of something else that could be done. It's very different from my first answer, as it doesn't use regular expressions, so I decided to make a second answer post. Use your language of choice's split() method equivalent on the string with the word to negate as the argument for what to split on. An example using Python:

Using Regular Expressions in Java, In other words: “regex” is applied as if you had written “^regex$” with start and end of string anchors. This is different myString.split("regex") splits the string at each regex match. In literal Java strings the backslash is an escape character. In most languages, when you feed this regex to the function that uses a regex pattern to split strings, it returns an array of words. Note that Python's re module does not split on zero-width matches—but the far superior regex module does. (direct link) Finding Overlapping Matches Sometimes, you need several matches within the same word.

Regular Expression HOWTO, You can also use REs to modify a string or to split it apart in various ways. As stated earlier, regular expressions use the backslash character ( '\' ) to indicate For example, if you wish to match the word From only at the beginning of a line,  Splitting a String. String splitting is another very important function. Strings can be split using split function from the re package. The split function returns a list of split tokens. Let's split a string of words where one or more space characters are found, as shown below:

6.2. re — Regular expression operations, Regular expressions use the backslash character ('\') to indicate special forms or to allow special Matches the empty string, but only at the beginning or end of a word. A word Note that split will never split a string on an empty pattern match. I am trying to write a regex for finding slashes only that are not followed by special characters. For example, if the string is, /PErs/@loc/g/2, then I regex should find slashes (/) that are before P, g and 2. It should not return slash before @ as @ is a special character. I could write \/\w but it is returning me /P, /g and /2.

  • So you don't want to split on / if it has digit before and after it. You can use mechanism here.
  • 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.
  • 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.
  • Can this be done without the groups? I mean that regex will mark the '/'
  • Well it will mark the '/' but if you want to see what's on the two side of the slash then you need to match against that also. And in order to use that later on it will require a group.
  • I don't need to look at it. It will split it anyway in my code
  • You need the regex to look at it ;) in order to see if it is word/number or whatever. If you don't need to look at it just split on "/"
  • there is no WordNumber in the language
  • @UnknownUser that's fine - it will still work anyway.
  • I prefer regex solution since I already using it to split other expressions