Regular Expression for (1ab09cs001) - java

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.

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)

Just try with followinf regex:
"^([^(]+) \\(([^)]+)\\)$"
Or better:
String originalString = "Computer Science (06cs43)";
String[] parts = originalString.split("\\(");
String subjectName = parts[0].trim();
String subjectCode = null;
if (parts.length > 1) {
subjectCode = parts[1].replaceAll("\\)$", "");
}

Related

Replace word from string in java

String[] operatorList = { "name", "first_name", "last_name", "city" };
String originalString = "city=Houston^ORlast_name=Cervantsz^ORfirst_name=John^name=don";
for (String opElement : operatorList) {
if (originalString.contains(opElement)) {
String tempStr = originalString.replace(opElement, "user." + opElement);
originalString = tempStr;
}
}
System.out.println("originalString " + originalString);
Output:
user.city=Houston^ORlast_user.name=Cervantsz^ORfirst_user.name=John^user.name=don
When i am trying to replace name with "user.name" at that time name from "last_name" is replaced with "last_user.name" and first_name with first_user.name
But i want replace "name" with "user.name" and "last_name" with "user.last_name"
and "first_name" with "user.first_name".
Any help appreciated.
You can add prefix all key and control that key. Example
String[] operatorList = {"name", "first_name", "last_name", "city"};
String originalString = "city=Houston^ORlast_name=Cervantsz^ORfirst_name=John^ORname=don";
for (String opElement : operatorList) {
if (originalString.contains("^OR"+opElement)) {
String tempStr = originalString.replace(opElement, "user." + opElement);
originalString = tempStr;
}
}
System.out.println("originalString " + originalString);
If the values you are trying to change are always unique and generated (meaning they are always in the same order), you can simply put your operators in the same order and use replaceLast() instead.
A more complete solution would be to determine how the string is constructed. Do all the values have a ^ in front of them? Is OR generated for the same values or is it to indicate optional values?. So in the end, what allows you to split the string properly. Then you can use a Regex to use the surrounding characters.
I would format the string to make sure the splitters are constant (all "^OR" become "##%!!" and all remaining "^" become "%!!") so all replaced strings start with !!. Then I would reformat the string to the original format using the remaining "##%" or "%" :
String[] operatorList = { "name", "first_name", "last_name", "city" };
String originalString = "city=Houston^ORlast_name=Cervantsz^ORfirst_name=John^name=don";
originalString = originalString.replaceAll("\\^OR", "##%!!");
originalString = originalString.replaceAll("\\^", "%!!");
//the order is important here
for (String opElement : operatorList) {
if (originalString.startsWith(opElement)) {
originalString = originalString.replaceFirst(opElement, "user." + opElement);
}
originalString = originalString.replaceAll("!!" + opElement, "user." + opElement);
// no need for an other alternative here because replaceAll returns the
// String as is if it does not find the first parameter in the String.
}
originalString = originalString.replaceAll("##%", "^OR");
originalString = originalString.replaceAll("%", "^");
// the order here is also important
outputs : "user.city=Houston^ORuser.last_name=Cervantsz^ORuser.first_name=John^user.name=don"
If all keypairs need prefix "user.", I would like to split originalString first.
In Java8
String originalString = "city=Houston^ORlast_name=Cervantsz^ORfirst_name=John^name=don";
String[] params = originalString.split("\\^");
String result = String.join("^", Arrays.stream(params)
.map(param -> param.startsWith("OR") ? "ORuser." + param.substring(2) : "user." + param)
.collect(Collectors.toList()));
System.out.println(result);
It can also be changed to for loop type.
You may use a quick search and replace with an alternation based pattern created dynamically from the search words only when they are preceded with a word boundary or ^ + OR/AND/etc. operators and followed with a word boundary. Note that this solution assumes the search words only consist of word chars (letters, digits or _):
String[] operatorList = { "name", "first_name", "last_name", "city" };
// assuming operators only consist of word chars
String pat = "(\\b|\\^(?:OR|AND)?)(" + String.join("|", operatorList) + ")\\b";
String originalString = "city=Houston^ORlast_name=Cervantsz^ORfirst_name=John^name=don";
originalString = originalString.replaceAll(pat, "$1user.$2");
System.out.println(originalString);
// => user.city=Houston^ORuser.last_name=Cervantsz^ORuser.first_name=John^user.name=don
See the Java demo online
The regex will look like
(\b|\^(?:OR|AND)?)(name|first_name|last_name|city)\b
See the regex demo.
Details
(\b|\^(?:OR|AND)?) - Group 1: a word boundary \b or a ^ symbol and an optional substring, OR or AND (you may add more here after |)
(name|first_name|last_name|city) - Group 2: any of the search words
\b - a trailing word boundary.
The $1 in the replacement pattern inserts the contents of Group 1 and $2 does the same with Group 2 contents.

Java regexp to remove Freemarker interpolation tags

I'm trying to create a regexp to remove Freemarker interpolation tags in a String. I've a template with text and interpolations as "Hi customer, we remember your appointment ${date?string["dd"]}"
I want remove/replate this interpolation tag that is a bit particular because has inside the question mark.
I tried to create the regexp in this way:
String myString = "Hi customer, we remember your appointment ${date?string["dd"]}"
myString = myString.replaceAll(Pattern.quote("${date?string[\"dd\"]}"), "xx");
but don't works. Where I'm making the mistake?
Don't forget to assign return value of replaceAll method to original string as replaceAll (or any other String API) doesn't change the underlying immutable String object:
String myString = "Hi customer, we remember your appointment ${date?string[\"dd\"]}";
myString = myString.replaceAll(Pattern.quote("${date?string[\"dd\"]}"), "xx");
//=> Hi customer, we remember your appointment xx
Using regex, you could do:
String myString = "Hi customer, we remember your appointment ${date?string[\"dd\"]}";
myString = myString.replaceAll("\\$\\{date\\?string\\[\"dd\"\\]\\}", "xx");
The result string is:
Hi customer, we remember your appointment xx
I'm dividing string into 4 logical groups and then assembling required content together.
Try following regex:
(.*?)(?:\$\{.*?\"([^\"]+).{3})(.*)
Example:
String text = "Hi customer, we remember your appointment ${date?string[\"dd\"]}";
String replacement_text = "xxx";
String rx = "(.*?)(?:\\$\\{.*?\"([^\"]+).{3})(.*)";
Pattern regex = Pattern.compile(rx, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(text);
String result = regexMatcher.replaceAll("$1" + replacement_text + "$3");
System.out.println(result);
Code will emit:
Hi customer, we remember your appointment xxx
However, if you want to extract content of marker i.e. dd, simply replace value of replacement_text with $2 and you'll get
Hi customer, we remember your appointment dd

How to split a long string in Java?

How to edit this string and split it into two?
String asd = {RepositoryName: CodeCommitTest,RepositoryId: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef};
I want to make two strings.
String reponame;
String RepoID;
reponame should be CodeCommitTest
repoID should be 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef
Can someone help me get it? Thanks
Here is Java code using a regular expression in case you can't use a JSON parsing library (which is what you probably should be using):
String pattern = "^\\{RepositoryName:\\s(.*?),RepositoryId:\\s(.*?)\\}$";
String asd = "{RepositoryName: CodeCommitTest,RepositoryId: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef}";
String reponame = "";
String repoID = "";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(asd);
if (m.find()) {
reponame = m.group(1);
repoID = m.group(2);
System.out.println("Found reponame: " + reponame + " with repoID: " + repoID);
} else {
System.out.println("NO MATCH");
}
This code has been tested in IntelliJ and runs without error.
Output:
Found reponame: CodeCommitTest with repoID: 425f5fc5-18d8-4ae5-b1a8-55eb9cf72bef
Assuming there aren't quote marks in the input, and that the repository name and ID consist of letters, numbers, and dashes, then this should work to get the repository name:
Pattern repoNamePattern = Pattern.compile("RepositoryName: *([A-Za-z0-9\\-]+)");
Matcher matcher = repoNamePattern.matcher(asd);
if (matcher.find()) {
reponame = matcher.group(1);
}
and you can do something similar to get the ID. The above code just looks for RepositoryName:, possibly followed by spaces, followed by one or more letters, digits, or hyphen characters; then the group(1) method extracts the name, since it's the first (and only) group enclosed in () in the pattern.

Java string split with multiple delimeters

What would be the best way to split this string directly after the CN= to store both the first and last name in separate fields as shown below?
String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com"
String firstName"Paul"
String lastName="Sebula"
Don't re-invent the wheel. Assuming these are well-formed DN's, see the accepted answer on this question for how to parse without directly writing your own regex: Parsing the CN out of a certificate DN
Once you've extracted the CN, then you can apply some of the other parsing techniques suggested (use the Java StringTokenizer or the String.split() method as others here have suggested if it's known to be separated only by spaces). That assumes that you can make assumptions (eg. the first element in the resulting array is the firstName,the last element is the lastName and everything in between is middle names / initials) about the CN format.
You can use split:
String distinguisedName = "CN=Paul Sebula,OU=BAE,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=baesystems,DC=com";
String[] names = distinguisedName.split(",")[0].split("=")[1].split(" ");
String firstName = names[0];
String lastName= names.length > 2 ? names[names.length-1] : names[1];
System.out.println(firstName + " " + lastName);
See IDEONE demo, output: Paul Sebula.
This also accounts for just 2 names (first and last only). Note how last name is accessed it being the last item in the array.
public static void main(String[] args) {
String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com";
String splitResult[]=distinguisedName.split(",")[0].split("=");
String resultTwo[]=splitResult[1].split("\\.");
String firstName=resultTwo[0].split(" ")[0].trim();
String lastName=resultTwo[1].trim();
System.out.println(firstName);
System.out.println(lastName);
}
output
Paul
Sebula
String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com"
String[] commaSplit = distinguisedName.split(',');
String[] whitespaceSplit = commaSplit[0].split(' ');
String firstName = whitespaceSplit[0].substring(3);
String lastName = whiteSpaceSplit[2];
In steps:
String distinguisedName = "CN=Paul M. Sebula,OU=BBB,OU=Users,OU=TIES Project,DC=SPHQTest,DC=na,DC=BBBBBB,DC=com";
String fullName = distinguisedName.substring(3, distinguisedName.indexOf(','));
String[] nameParts = fullName.split(" ");
String firstName = nameParts[0];
String lastName = nameParts[nameParts.length-1];
This will work for cases where the middle name/initial are not present as well.

Java regular expression to extract key value pairs

How can I extract the value of the bookid from this string using Java?
href="http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45">cooking book
Normally you should use some parser but if your String is really this short then maybe regular expression can be option like
String s="href=\"http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45\">cooking book";
Pattern p= Pattern.compile("(?<=bookid=)\\d+");
Matcher m=p.matcher(s);
if (m.find())
System.out.println(m.group());
output:
12345678
A very simple answer would be
String[] queryParams = url.split("\\?")[1].split("&");
This would give you all the parameters in a=b form in each of the element. You can then just split the needed param.
But ideally you should extract the value by param name
Pshemo you beat me to it but you can also use this:
"(id\=[0-9]*)"
and try RegexPlanet to try out your regex and retrieve the escapped string in java format
You can use the following code snippet:
String str = "href=\"http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45\">cooking book";
String[] strArray = str.split("&");
String bookId = "";
for(int i=0;i<strArray.length;i++)
{
if(strArray[i].startsWith("bookid"))
{
bookId = strArray[i].split("=")[1];
}
}
System.out.println("Book ID = "+bookId);

Categories

Resources