Split the string with hyphen symbol multiple occurrence using regex /java - java

Getting the string value using the below xpath
String noAndDate = driver.findElement(By.xpath("//*[#id='c38']/div/table/tbody/tr[1]/td/strong")).getText();
Output of the above string = 2928554 - 2009-09-18 (BOPI 2009-38)
my expected output
2928554
2009-09-18
i tried below split, but i'm not getting my expected output
String[] words = noAndDate.split("-");
Please advice/help me

You can instead try splitting on a regex alternation which looks for a hyphen surrounded by whitespace, or pure whitespace:
String input = "2928554 - 2009-09-18 (BOPI 2009-38)";
String[] parts = input.split("(\\s+-\\s+|\\s+)");
System.out.println(parts[0]);
System.out.println(parts[1]);
Demo

Try the below code-
String str = "2928554 - 2009-09-18 (BOPI 2009-38)";
String str1 = str.split(" - | ")[0];
String str2 = str.split(" - | ")[1];
This will return str1 as 2928554 and str2 as 2009-09-18.
Hope this will help you !

Just split with regex will do.
String given = "2928554 - 2009-09-18 (BOPI 2009-38)";
String [] splitted = given.split(" - |\\s+");
String result = splitted[0] +", "+splitted[1];
System.out.println(result);
prints
2928554, 2009-09-18

Use Regex capture groups, here you can see what you want in 2 groups:
(\d+)\s*-\s*(\d+\-\d+\-\d+)
() = group

Try this:
String[] words = noAndDate.split(" ");
then
System.out.println(words[0]);
System.out.println(words[2]);

Related

How to split a string having [] as delimiters in java

I want to remove [ ] braces from the below string-
"[maths=100, english=20]"
I have tried doing it in following ways but in both the trials it is not removing the end ] brace.
Approach 1:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[");
String[] finalValue = value[1].split("\\]");
System.out.println(finalValue[0]); // this should give string as maths=100, english=20
Approach 2:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[\\]");
System.out.println(finalValue[1]); // this should give string as maths=100, english=20
Can anyone guide me where i am doing it wrong?
Try this code
String metrics= "[maths=100, english=20]";
String[] split = metrics.split("\\[|]");
System.out.println(split[1]);
it prints
"maths=100, english=20"
Or you can simply replace all [ and ] character
String metrics = "[maths=100, english=20]";
metrics = metrics.replace("[", "").replace("]", "");
System.out.println(metrics);
If you simply want to trim and clean your data then you can do a simple check and substring.
String input = ...;
String cleanedInput = input.trim();
if (cleanedInput.startsWith("[") && cleanedInput.endsWith("]")) {
cleanedInput = cleanedInput.substring(1, cleanedInput.length() - 1);
System.out.println(cleanedInput);
}
If you're wanting to match and capture from a larger set of data then you can use RegEx patterns with capture groups to match and capture the data you want.
For parsing a proper document structure though you should try to use a real parser but if you truly are just trying to match and capture some simple data then RegEx will often be ok.
String input = ...;
// RegEx pattern "\[([^\[\]]*)\]" anything inside braces except other braces
Pattern pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");
Matcher matcher = pattern .matcher(input);
while (matcher.find()) {
String data = matcher.group(1);
System.out.println(data);
}
You can simply replace the braces like this:
String s = "[maths=100, english=20]";
s = s.replace("[", "").replace("]", "");
System.out.println(s);

split the special charater contain inside the string

I have a String """JBL#gmail.com from which I want to remove the """ which is located at the front of the email address. I tried to use split, but unfortunately it didn't work.
Here is my code:
String [] sender1 = SA1.split(" ");
String str1 = sender1[0];
System.out.println("the str1 is :"+str1);
String [] sender2 = str1.split("\\\"");
String str2 = sender2[0];
String str3 = sender2[1];
System.out.println("the str2 is :"+str2);
System.out.println("the str3 is :"+str3);
Here is my code output-
the str1 is :"""JBL#gmail.com""
the str2 is :
the str3 is :
My SA1 will contain """JBL#gmail.com"" <JBL#gmail.com>". The email address can be a mix of lower/upper case letters, numbers, and etc.
If SA1 does in fact contain
"\"\"\"JBL#gmail.com\"\" <JBL#gmail.com>\""
then you can use Pattern/Matcher with a Regular Expression of "<(.*?)>" to retrieve the E-Mail Address from the String:
String sa1 = "\"\"\"JBL#gmail.com\"\" <JBL#gmail.com>\"";
String email = "";
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(sa1);
while (matcher.find()) {
// Is a match found?
if (!matcher.group(1).equals("")) {
// There is so place the match into the
// email variable.
email = matcher.group(1);
}
}
// Display the E-Mail Address in Console Window.
System.out.println("E-Mail Address is: " + email);
Console window will display:
E-Mail Address is: JBL#gmail.com
Regular Expression Explanation:
You can obtain the email in the first part of the string by removing all quotation marks (replace("\"", "")), spliting by spaces (split(" ")), and taking the first element in the split ([0]):
String str = "\"\"\"JBL#gmail.com\"\" <JBL#gmail.com>\"";
str.replace("\"", "").split(" ")[0];
Note that the second element would produce <JBL#gmail.com>.
"fdsd\"\"\" dsd".split("\"\"\"")
you have to use
"yourWords".split("\"\"\"")
String s= "\"\"\"JBL#gmail.com\"\" <JBL#gmail.com>\"".
split("<")[1].replace(">", "").replace("\"", "");

Java: removing the first word in split

i want to split a String by whitespaces and remove the First match.
Since doing this seperatly would be in O(n) i wonder if there is a Regex for doing this?
e.g.:
String s = "asd wer gfb";
String sA[] = s.split(magixRegex);
than sA should contain:
["wer", "gfb"]
Replace the first word and then do splitting.
String s = "asd wer gfb";
String sA[] = s.replaceFirst("^\\S+\\s*", "").split("\\s+");
System.out.println(Arrays.toString(sA));
You could substring it first:
String s = "asd wer gfb";
s = s.substring( s.indexOf(' ') + 1 );
String sA[] = s.split(" ");

Java regex replace all periods before first colon

Example input:
this.is:an-example:3.0.3
I need to replace the periods before the first colon with forward slashes, as well as every colon with a forward slash.
Needed output:
this/is/an-example/3.0.3
Try this:
// sample input
String s1 = "this.is:an-example:3.0.3";
// `s2` contains the desired output
int idx = s1.lastIndexOf(':') + 1;
String s2 = s1.substring(0, idx).replace('.', '/').replace(':', '/') + s1.substring(idx);
// now we test it
System.out.println(s2);
=> this/is/an-example/3.0.3
String input = "this.is:an-example:3.0.3:";
input = input.replaceAll("^([^:]*)\\.(?=[^:]*:)|:", "$1/");
Output:
this/is/an-example/3.0.3/
You can use this replacement:
String result = yourstr.replaceAll("\\.(?![^:]*$)|:", "/");

Cut ':' && " " from a String with a tokenizer

right now I am a little bit confused. I want to manipulate this string with a tokenizer:
Bob:23456:12345 Carl:09876:54321
However, I use a Tokenizer, but when I try:
String signature1 = tok.nextToken(":");
tok.nextToken(" ")
I get:
12345 Carl
However I want to have the first int and the second int into a var.
Any ideas?
You have two different patterns, maybe you should handle both separated.
Fist you should split the space separated values. Only use the string split(" "). That will return a String[].
Then for each String use tokenizer.
I believe will works.
Code:
String input = "Bob:23456:12345 Carl:09876:54321";
String[] words = input.split(" ")
for (String word : words) {
String[] token = each.split(":");
String name = token[0];
int value0 = Integer.parseInt(token[1]);
int value1 = Integer.parseInt(token[2]);
}
Following code should do:
String input = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer st = new StringTokenizer(input, ": ");
while(st.hasMoreTokens())
{
String name = st.nextToken();
String val1 = st.nextToken();
String val2 = st.nextToken();
}
Seeing as you have multiple patterns, you cannot handle them with only one tokenizer.
You need to first split it based on whitespace, then split based on the colon.
Something like this should help:
String[] s = "Bob:23456:12345 Carl:09876:54321".split(" ");
System.out.println(Arrays.toString(s ));
String[] so = s[0].split(":", 2);
System.out.println(Arrays.toString(so));
And you'd get this:
[Bob:23456:12345, Carl:09876:54321]
[Bob, 23456:12345]
If you must use tokeniser then I tink you need to use it twice
String str = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer spaceTokenizer = new StringTokenizer(str, " ");
while (spaceTokenizer.hasMoreTokens()) {
StringTokenizer colonTokenizer = new StringTokenizer(spaceTokenizer.nextToken(), ":");
colonTokenizer.nextToken();//to igore Bob and Carl
while (colonTokenizer.hasMoreTokens()) {
System.out.println(colonTokenizer.nextToken());
}
}
outputs
23456
12345
09876
54321
Personally though I would not use tokenizer here and use Claudio's answer which splits the strings.

Categories