0

I am very new programmer to Java regular expressions. I do not want to use java split with delimiters and try getting the individual tokens. I don't feel its a neat way. I have the following string

"Some String lang:c,cpp,java file:build.java"

I want to break up this into three parts

1 part containing "Some String" 
2 part containing "c,cpp,java" 
3 String containing "build.java"

The lang: and file: can be placed any where and they are optional.

2
  • 2
    Why not split? It's certainly not bad. Commented Apr 18, 2011 at 12:43
  • Well I feel split makes it more difficult to maintain Commented Apr 18, 2011 at 12:45

3 Answers 3

1

The lang: and file: can be placed any where and they are optional.

Try the following expressions to get the language list and the file:

String input = "Some String lang:c,cpp,java file:build.java";
String langExpression = "lang:([\\w,]*)";
String fileExpression = "file:([\w\.]*)";

Patter langPattern = Pattern.compile(langExpression);
Matcher langMatcher = langPattern.matcher(input);
if (langMatcher.matches()) {
  String languageList = langMatcher.group(1);
}

Patter filePattern = Pattern.compile(fileExpression );
Matcher fileMatcher = filePattern.matcher(input);
if (fileMatcher .matches()) { 
  String filename= fileMatcher.group(1);
}

This should work with lang:xxx file:xxx as well as file:xxx lang:xxx as long as the language list or the filename don't contain whitespaces. This would also work if lang: and/or file: was missing.

Would you also expect a string like this: file:build.java Some String lang:c,cpp,java?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Thomas, I will try this . This will be a very good head start to get regex working for me.
@Nandish You could also have a look at regular-expressions.info which IMHO is a very good reference.
1

What is so "unmaintainable" about using split?

        String str = "Some String lang:c,cpp,java file:build.java";
        String[] s  = str.split("(lang|file):");

Comments

0

While split can do what you want to achieve, you can write your own code using substring and indexOf methods. This will be far faster than using split in terms of performance.

1 Comment

If the OP is concerned about maintainability, I guarantee that rolling your own variant of split using substring and indexOf is not going to help in that regard. Furthermore, this smells of premature optimization to me.

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.