Search code examples
javaregexreplaceall

Regular Expression for (1ab09cs001)


I want to separate string i.e combination of Subject Name with Subject Code into two parts in java. The originalString can be ANYTHING like "ABC (01dfv)" , "BCD (sdfsd) etc... The subject code always written in () and subject name will always prefix the bracket. Example :

String originalString = "Computer Science (06cs43)"
String subjectName="Computer Science"
String subjectCode="06cs43"

I am using string.replaceAll but not able to find out the regular expression for extracting or replacing the subject code.

The size of the subject code is not fixed.


Solution

  • No need to use regex here. You can just do this

    String orS="Computer Science (06cs43)";
    String subjectName=orS.subString(0,orS.indexOf('(')-1);
    String subjectCode=orS.subString(orS.indexOf('('),orS.length()-2)