How to get String between last two underscore - java

I have a string "abcde-abc-db-tada_x12.12_999ZZZ_121121.333"
The result I want should be 999ZZZ
I have tried using:
private static String getValue(String myString) {
Pattern p = Pattern.compile("_(\\d+)_1");
Matcher m = p.matcher(myString);
if (m.matches()) {
System.out.println(m.group(1)); // Should print 999ZZZ
}
else {
System.out.println("not found");
}
}

If you want to continue with a regex based approach, then use the following pattern:
.*_([^_]+)_.*
This will greedily consume up to and including the second to last underscrore. Then it will consume and capture 9999ZZZ.
Code sample:
String name = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
Pattern p = Pattern.compile(".*_([^_]+)_.*");
Matcher m = p.matcher(name);
if (m.matches()) {
System.out.println(m.group(1)); // Should print 999ZZZ
} else {
System.out.println("not found");
}
Demo

Using String.split?
String given = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String [] splitted = given.split("_");
String result = splitted[splitted.length-2];
System.out.println(result);

Apart from split you can use substring as well:
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String ss = (s.substring(0,s.lastIndexOf("_"))).substring((s.substring(0,s.lastIndexOf("_"))).lastIndexOf("_")+1);
System.out.println(ss);
OR,
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String arr[] = s.split("_");
System.out.println(arr[arr.length-2]);

The get text between the last two underscore characters, you first need to find the index of the last two underscore characters, which is very easy using lastIndexOf:
String s = "abcde-abc-db-tada_x12.12_999ZZZ_121121.333";
String r = null;
int idx1 = s.lastIndexOf('_');
if (idx1 != -1) {
int idx2 = s.lastIndexOf('_', idx1 - 1);
if (idx2 != -1)
r = s.substring(idx2 + 1, idx1);
}
System.out.println(r); // prints: 999ZZZ
This is faster than any solution using regex, including use of split.

As I misunderstood the logic from the code in question a bit with the first read and in the meantime there appeared some great answers with the use of regular expressions, this is my try with the use of some methods contained in String class (it introduces some variables just to make it more clear to read, it could be written in the shorter way of course) :
String s = "abcde-abc-db-ta__dax12.12_999ZZZ_121121.333";
int indexOfLastUnderscore = s.lastIndexOf("_");
int indexOfOneBeforeLastUnderscore = s.lastIndexOf("_", indexOfLastUnderscore - 1);
if(indexOfLastUnderscore != -1 && indexOfOneBeforeLastUnderscore != -1) {
String sub = s.substring(indexOfOneBeforeLastUnderscore + 1, indexOfLastUnderscore);
System.out.println(sub);
}

Related

Extract string between curly braces in java

I have String {a,b,c},{1,2,3}. How to extract to get like
String1 = a,b,c;
String2 = 1,2,3;
I have tried someting like this, but that does not work.
String result1 = str.substring(str.indexOf("{") + 1, str.indexOf("},"));
String result2 = str.substring(str.indexOf(",{") + 1, str.indexOf("}"));
There is an indexOf method that takes the index from which to search
String str = "{a,b,c},{1,2,3}";
int startingIndex = str.indexOf("{");
int closingIndex = str.indexOf("}");
String result1 = str.substring(startingIndex + 1, closingIndex);
System.out.println(result1);
startingIndex = str.indexOf("{", closingIndex + 1);
closingIndex = str.indexOf("}", closingIndex + 1);
String result2 = str.substring(startingIndex + 1, closingIndex);
System.out.println(result2);
In the second block, we make the search start at closingIndex + 1 where closingIndex is the index of the last seen }.
You can use a regular expression for that e.g.
Matcher m = Pattern.compile("(?<=\\{).+?(?=\\})").matcher("{a,b,c},{1,2,3}");
while(m.find()){
System.out.println(m.group());
// a,b,c
// 1,2,3
}
One of the possible solutions for this task will be using Pattern class.
It should be a much more elegant solution for getting values between braces.
For matching the first group regex should be: \{([^}]*)\}.
Just add a separator between group , and repeat regex again. Now you can get results separately.
Here is code demo:
public class RegexDemo {
public static void main(String[] args) {
String str = "{a,b,c},{1,2,3}";
Pattern pattern = Pattern.compile("\\{([^}]*)\\},\\{([^}]*)\\}");
Matcher matcher = pattern.matcher(str);
String first = null;
String second = null;
if (matcher.find()) {
first = matcher.group(1);
second = matcher.group(2);
}
System.out.printf("First: %s\nSecond: %s\n", first, second);
}
}
Output:
First: a,b,c
Second: 1,2,3

Java replaceAll ' with '' except first and last occurrence

I want to replace all occurrences of single quotations with two quotations, except in first and last occurrence, I managed to get to exclude the last occurrence using regex as follows
String toReplace = "'123'456'";
String regex = "'(?=.*')";
String replaced = toReplace.replaceAll(regex,"''");
System.out.println(replaced);
Here I get
''123''456'
How do I get
'123''456'
Thank you.
There is a pithy saying about regular expressions and two problems, but I'll skip that and suggest you simplify this by using a StringBuilder; find the index of both the first ' and the last ' in your input, then iterate between those indices looking for ' (and replacing with ''). Something like,
StringBuilder sb = new StringBuilder(toReplace);
int first = toReplace.indexOf("'"), last = toReplace.lastIndexOf("'");
if (first != last) {
for (int i = first + 1; i < last; i++) {
if (sb.charAt(i) == '\'') {
sb.insert(i, '\'');
i++;
}
}
}
toReplace = sb.toString();
int first = toReplace.indexOf("'") + 1;
int last = toReplace.lastIndexOf("'");
String afterReplace = toReplace.substring(0, first)
+ toReplace.substring( first,last ).replaceAll("'", "''")
+ toReplace.substring(last);
System.out.println(afterReplace);
With StringBuilder
String afterReplace = new StringBuilder()
.append(toReplace, 0, first)
.append(toReplace.substring(first, last).replaceAll("'", "''"))
.append(toReplace, last, toReplace.length())
.toString();
Or with String.format
String afterReplace = String.format("%s%s%s",
toReplace.substring(0, first),
toReplace.substring(first, last).replaceAll("'", "''"),
toReplace.substring(last));
Regex expression: (?<=')(. *)(?=')
It will help you to find out your result and then you can replace it.
This can be achieved with regex as well:
// String to be scanned to find the pattern.
String line = "'123'456'";
String pattern = "(?>^')(.*)(?>'$)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(1) );
String replaced = m.group(1).replaceAll("'","\"");
System.out.println("replaced value: " + replaced );
}else {
System.out.println("NO MATCH");
}

java string replaceall first character after certain string to lower case

I have a requirement to replace all the character within a string to lower case if it is followed by some string like "is".
For example:
String a = "name=xyz,isSalaried=Y,address=abc,isManager=N,salary=1000";
it should get converted to
"name=xyz,salaried=Y,address=abc,manager=N,salary=1000"
I am not very good at regular expression but I think can use it to achieve the required output.
It will be great if someone can help me out.
Your solution requires basic understanding of String and String methods in java.
Here is one working example. Although, it might not be the most efficient one.
NOTE:- YOU ASKED FOR A REGEX SOLUTION.BUT THIS IS USING PURE STRING METHODS
public class CheckString{
public static void main(String[] ar){
String s = "name=xyz,isSalaried=Y,address=abc,isManager=N,salary=1000";
String[] arr = s.split(",");
String ans = "";
int i = 0;
for(String text : arr){
int index = text.indexOf("=");
String before = text.substring(0,index).replace("is","").toLowerCase();
String after = text.substring(index);
if(i!=(arr.length-1)){
ans += before + after + ",";
i++;
}
else{
ans += before + after;
}
}
System.out.println(ans);
}
}
Try this.
first match the string and replace in a loop
String a = "name=xyz,isSalaried=Y,address=abc,isManager=N,salary=1000";
Matcher matcher = Pattern.compile("is(.*?)=").matcher(a);//.matcher(a).replaceAll(m -> m.group(1).toLowerCase());
while (matcher.find()) {
String matchedString = matcher.group(1);
a = a.replace("is"+matchedString,matchedString.toLowerCase());
}
System.out.printf(a);

How to cut out specific pieces of a string

So I have a string that has several start markers and end markers. How can I make a code that only keeps segments that are between the start and end markers?
A good example would be DNA transcription.
So the starting marker would be TAC, and an end marker would be ACT.
I have a string: AGATACACGACTAGCGAGCTACGATACTACC.
I know how to use the substring method, but not well enough so that it cuts the string down to:
TACACGACTTACGATACT.
How can I do this?
EDIT: I have solved this problem by writing this method:
private String spliceString(String n){
int counter1 = 0;
int startloc = 0;
int endloc = 0;
String m = "";
while (n.indexOf("TAC",counter1) != -1){
startloc = n.indexOf("TAC",counter1);
if (n.indexOf("ACT", counter1) != -1){
endloc = n.indexOf("ACT", counter1);
}
else if (n.indexOf("ATT", counter1) != -1){
endloc = n.indexOf("ATT", counter1);
}
else if (n.indexOf("ATC", counter1) != -1){
endloc = n.indexOf("ATC", counter1);
}
else {
return "AAAA"; //Returns a error string. This will be caught in another method that is not relevant.
}
m = m + n.substring(startloc,endloc + 3);
counter1 = endloc + 1;
}
System.out.println(m); //Just prints out so to check if the code worked
return m;
}
For this, regular expression is your friend.
One way would be to search for what you want to keep, and collect that in a StringBuilder.
String input = "AGATACACGACTAGCGAGCTACGATACTACC";
StringBuilder buf = new StringBuilder();
Matcher m = Pattern.compile("TAC.*?ACT").matcher(input);
while (m.find())
buf.append(m.group());
String output = buf.toString();
System.out.println(output); // prints: TACACGACTTACGATACT
See IDEONE for running code.
Read the javadoc of Pattern for more information on regex.
Alternatively, delete what you don't want to keep, i.e.
Text before first TAC
Text between ACT and TAC
Text after last ACT
The code is much simpler, but the regex is a bit more complex:
String input = "AGATACACGACTAGCGAGCTACGATACTACC";
String output = input.replaceAll("(?<=^|ACT).*?(?=TAC|$)", "");
System.out.println(output); // prints: TACACGACTTACGATACT
See regex101.com for nice color-coded example.
Java - String substring() Method
Description:
This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 If second argument is given.
Syntax:
Here is the syntax of this method:
public String substring(int beginIndex)
or
public String substring(int beginIndex, int endIndex)
Parameters:
Here is the detail of parameters:
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclusive.
Return Value:
The specified substring.
Example:
import java.io.*;
public class Test{
public static void main(String args[]){
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );
}
}
This produces the following result:
Return Value : Tutorialspoint.com
Return Value : Tuto

How to get with JAVA a specific value for one substring from string?

I have ONE string field which is in format:
"TransactionID=30000001197169 ExecutionStatus=6
additionalCurrency=KMK
pin= 0000"
So they are not separated with some ; оr , they are not seperated even with one blank space.
I want to get value for Execution Status and put it in some field?
How to achieve this?
Thanks for help
This works. But I am not sure this is the most optimal.It just solves your problem.
String s = "TransactionID=30000001197169ExecutionStatus=6additionalCurrency=KMKpin=0000";
if(s!=null && s.contains("ExecutionStatus="))
{
String s1[] = s.split("ExecutionStatus=");
if(s1!=null && s1.length>1)
{
String line = s1[1];
String pattern = "[0-9]+";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Match");
System.out.println("Found value: " + m.group(0) );
} else {
System.out.println("NO MATCH");
}
}
}
In your example they are indeed seperated by blanks, but the following should be working without blanks, too. Assuming your String is stored in String arguments
String executionStatus;
String[] anArray = arguments.split("=");
for (int i; i < anArray.length; i++)
if (anArray[i].contains("ExecutionStatus")){
executionStatus = anArray[++i].replace("additionalCurrency","");
executionStatus = executionStatus.trim();
}
}
Check if it contains() ExecutionStatus=
If yes then split the string with ExecutionStatus=
Now take the Second string from array find the first occurance of non digit char and use substring()
Assuming all that white space is present in your string, this works.
String str = "\"TransactionID=30000001197169 ExecutionStatus=6\n" +
" additionalCurrency=\"KMK\"\n" +
" pin= \"0000\"\"";
int start = str.indexOf("ExecutionStatus=") + "ExecutionStatus=".length();
int status = 0;
if (start >= 0) {
String strStatus = str.substring(start, str.indexOf("additionalCurrency=") - 1);
try {
status = Integer.parseInt(strStatus.trim());
} catch (NumberFormatException e) {
}
}
At the risk of attracting "... and now you have two problems!" comments, this is probably easiest done with regexes (str is the String defined above):
Pattern p = Pattern.compile("ExecutionStatus\\s*=\\s*(\\d+)"); // Whitespace matching around equals for safety, capturing group around the digits of the status)
Matcher m = p.matcher(str);
String status = m.find() ? m.group(1) : null;

Categories