How to split a string by 2 strings in Java - java

How do I split this string by two words?
<input type="hidden" name="SYNCHRONIZER_TOKEN" value="2f56248e-e54d-48ef-8c8c-6028d6f3d63f" id="SYNCHRONIZER_TOKEN" />
String 1: value="
String 2: " id="SYNC
After every split the string need to look like: 2f56248e-e54d-48ef-8c8c-6028d6f3d63f

Try using a regex to extract the value of interest. This way your code does not make any assumptions and will not break if there is something completely different after value=...
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SplitString {
public static void main(String[] args){
String input = "<input type=\"hidden\" name=\"SYNCHRONIZER_TOKEN\" value=\"2f56248e-e54d-48ef-8c8c-6028d6f3d63f\" id=\"SYNCHRONIZER_TOKEN\" />\n";
Pattern pattern = Pattern.compile("value=\"[a-zA-Z0-9-]+\"");
Matcher matcher = pattern.matcher(input);
if (matcher.find()){
String keyValue = matcher.group(0);
String key = keyValue.split("=")[0];
String value = keyValue.split("=")[1];
System.out.println("KeyValue: " + keyValue);
System.out.println("Key: " + key);
System.out.println("Value: " + value);
}
}
}
The output looks like this
KeyValue: value="2f56248e-e54d-48ef-8c8c-6028d6f3d63f"
Key: value
Value: "2f56248e-e54d-48ef-8c8c-6028d6f3d63f"

Related

Regex to split a string using java

I am trying to parse a string as I need to pass the map to UI.
Here is my input string :
"2020-02-01T00:00:00Z",1,
"2020-04-01T00:00:00Z",4,
"2020-05-01T00:00:00Z",2,
"2020-06-01T00:00:00Z",31,
"2020-07-01T00:00:00Z",60,
"2020-08-01T00:00:00Z",19,
"2020-09-01T00:00:00Z",10,
"2020-10-01T00:00:00Z",33,
"2020-11-01T00:00:00Z",280,
"2020-12-01T00:00:00Z",61,
"2021-01-01T00:00:00Z",122,
"2021-12-01T00:00:00Z",1
I need to split the string like this :
"2020-02-01T00:00:00Z",1 : split[0]
"2020-04-01T00:00:00Z",4 : split[1]
Issue is I can't split it on " , " as its repeated 2 times.
I need a regex that gives 2020-02-01T00:00:00Z,1 as one token to process further.
I am new to regex. Can someone please provide a regex expression for the same.
If you want the pairs of date-time and ID, you can use the regex, (\"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\",\d+)(?=,|$) to get the match results.
The pattern, (?=,|$) is the lookahead assertion for comma or end of the line.
Demo:
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String s = "\"2020-02-01T00:00:00Z\",1,\n"
+ " \"2020-04-01T00:00:00Z\",4,\n"
+ " \"2020-05-01T00:00:00Z\",2,\n"
+ " \"2020-06-01T00:00:00Z\",31,\n"
+ " \"2020-07-01T00:00:00Z\",60,\n"
+ " \"2020-08-01T00:00:00Z\",19,\n"
+ " \"2020-09-01T00:00:00Z\",10,\n"
+ " \"2020-10-01T00:00:00Z\",33,\n"
+ " \"2020-11-01T00:00:00Z\",280,\n"
+ " \"2020-12-01T00:00:00Z\",61,\n"
+ " \"2021-01-01T00:00:00Z\",122,\n"
+ " \"2021-12-01T00:00:00Z\",1";
List<String> list = Pattern.compile("(\\\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z\\\",\\d+)(?=,|$)")
.matcher(s)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
list.stream()
.forEach(p -> System.out.println(p));
}
}
Output:
"2020-02-01T00:00:00Z",1
"2020-04-01T00:00:00Z",4
"2020-05-01T00:00:00Z",2
"2020-06-01T00:00:00Z",31
"2020-07-01T00:00:00Z",60
"2020-08-01T00:00:00Z",19
"2020-09-01T00:00:00Z",10
"2020-10-01T00:00:00Z",33
"2020-11-01T00:00:00Z",280
"2020-12-01T00:00:00Z",61
"2021-01-01T00:00:00Z",122
"2021-12-01T00:00:00Z",1
Why can't you just split on , and ignore the last value?
Here's your pattern:
final Pattern pattern = Pattern.compile("(\\S+),(\\d+)");
final Matcher matcher = pattern.matcher("Input....");
Here's how to use it:
while (matcher.find()) {
final String date = matcher.group(1);
final String number = matcher.group(2);
}

How can I get the string element from the format string using regular expression?

My input string is like this :
String msgs="<InfoStart>\r\n"
+ "id:1234\r\n"
+ "phone:912119882\r\n"
+ "info_type:1\r\n"
+<InfoEnd>\r\n"
+"<InfoStart>\r\n"
+ "id:5678\r\n"
+ "phone:912119881\r\n"
+ "info_type:1\r\n"
+<InfoEnd>\r\n";
Now I can use the regular expression to get the info array :
private static Pattern patter= Pattern.compile("InfoStart>([\\s\\S]*?)<InfoEnd>");,But how to get the id,phone using regular expression?I try to write the code,but it fail,how to fix it?
private static Pattern infP = Pattern.compile("<InfoStart>([\\s\\S]*?)<InfoEnd>");
private static Pattern lineP = Pattern.compile(".*?\r\n");
final java.util.regex.Matcher matcher = patter.matcher(msgs);
while (matcher.find()){
String item = matcher.group(1);
Matcher matcherLine = lineP.matcher(item);
while(matcherLine.find()){
if(matcherLine.groupCount()>0){
String value= matcherLine.group(1);
int firstIndex=value.indexOf(":");
System.out.println("key:"+value.substring(0, firstIndex)+"value:"+value.substring(firstIndex+1));
}
}
}
Perhaps you can try this:
Pattern xmlPattern = Pattern.compile("<InfoStart>\\s+id:(\\d+)\\s+phone:(\\d+)\\s+info_type:(\\d+)\\s+<InfoEnd>");
Matcher matcher = xmlPattern.matcher(msgs);
while (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
}
The output:
1234
912119882
1
5678
912119881
1
But still I have to as say as Tim Biegeleisen mentioned, you'd better use other way around to parse a XML string.
Besides, your input string is incorrect, it should be:
String msgs="<InfoStart>\r\n"
+ "id:1234\r\n"
+ "phone:912119882\r\n"
+ "info_type:1\r\n"
+ "<InfoEnd>\r\n" // you lack an open double quote;
+"<InfoStart>\r\n"
+ "id:5678\r\n"
+ "phone:912119881\r\n"
+ "info_type:1\r\n"
+ "<InfoEnd>\r\n"; // you lack an open double quote;

How to avoid replacing specific words in a text in java

I have a method like this :
for(String abc:abcs){
xyz = abc.replaceAll(abc+"\\(", "_"+abc+"\\(");
}
How to avoid replacing few replacements which have specific prefixes for them in java
I tried this :
String data = "Today, abc.xyz is object oriented language";
String regex = "(?<!abc.)xyz";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(data);
System.out.println(matcher.find());
Does this work for you?
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String prefix = "abc";
String replaceWith = " text";
String input = "This xyz is an example xyz to show how you can replace certains values of the xyz.\n"
+ "The xyz can conain any arbitrary xyz, for example abc.xyz.";
Pattern pattern = Pattern.compile("[^" + prefix + "].xyz");
Matcher m = pattern.matcher(input);
while (m.find()) {
input = input.replace(m.group().substring(1), replaceWith);
}
System.out.println(input);
}
}

Best way to split a string containing question marks and equals

Having an issue where I have a java string:
String aString="name==p==?header=hello?aname=?????lname=lastname";
I need to split on question marks followed by equals.
The result should be key/value pairs:
name = "=p=="
header = "hello"
aname = "????"
lname = "lastname"
The problem is aname and lname become:
name = ""
lname = "????lname=lastname"
My code simply splits by doing aString.split("\\?",2)
which will return 2 strings.One contains a key/value pair and the second string contains
the rest of the string. If I find a question mark in the string, I recurse on the second string to further break it down.
private String split(String aString)
{
System.out.println("Split: " + aString);
String[] vals = aString.split("\\?",2);
System.out.println(" - Found: " + vals.length);
for ( int c = 0;c<vals.length;c++ )
{
System.out.println(" - "+ c + "| String: [" + vals[c] + "]" );
if(vals[c].indexOf("?") > 0 )
{
split(vals[c]);
}
}
return ""; // For now return nothing...
}
Any ideas how I could allow a name of ?
Disclaimer: Yes , My Regex skills are very low, so I don't know if this could be done via a regex expression.
You can let regex do all the heavy lifting, first splitting your string up into pairs:
String[] pairs = aString.split("\\?(?!\\?)");
That regex means "a ? not followed by a ?", which gives:
[name==p==, header=hello, aname=????, lname=lastname]
To then also split the results into name/value, split only the first "=":
String[] split = pair.split("=", 2); // max 2 parts
Putting it all together:
String aString = "name==p==?header=hello?aname=?????lname=lastname";
for (String pair : aString.split("\\?(?!\\?)")) {
String[] split = pair.split("=", 2);
System.out.println(split[0] + " is " + split[1]);
}
Output:
name is =p==
header is hello
aname is ????
lname is lastname
You can try like this
String[] vals = "Hello??Man?HowAreYou????".split("\\?+");
System.out.println(vals[0]+vals[1]+vals[2]);
OUTPUT
HelloManHowAreYou
But as aname=????? you want to get you can replace the
????? Five Question Marks with Other Symbol and replace back to ????? after split
String processed="Hello????Good? ? ....???".replace("????","*");
OUTPUT
Hello*Good? ? ....???
And than use split for ?
Here the code, you are looking .
Implemented using the Split and HashMap.
Tested and Executed.
import java.util.HashMap;
import java.util.Map;
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
// String[] vals = "Hello??Man?HowAreYou????".split("\\?+");
// System.out.println(vals[0]+vals[1]+vals[2]);
String query="name==p==?header=hello?aname=?????lname=lastname";
String[] params = query.split("\\?");
Map<String, String> map = new HashMap<String, String>();
for (String param : params)
{
String name = param.split("=")[0];
String value = param.substring(name.length(),param.length());
map.put(name, value);
System.out.println(name);
if(name.equals("")){
value+="?";
}
System.out.println(value.replaceAll(" ", ""));
}
}
}
I assume you are parsing URLs. The correct way would be to encode all special characters like ?, & and = which are values or names.
Better Solution: Encoding characters:
String name = "=p==";
String aname = "aname=????";
String lname = "lastname";
String url = "name=" + URLEncoder.encode(name, "UTF-8") +
"?aname=" + URLEncoder.encode(aname, "UTF-8") +
"?lname=" + URLEncoder.encode(lname, "UTF-8");
After that you have something like this:
name==p==?aname=?????lname=lastname
This can be splitted and decoded easily.
Other Solution: Bad input parsing:
If you insist, this works also. You can use a regex:
Pattern pattern = Pattern.compile("(\\w+?)=(\\S+?\\?+)");
Matcher m = pattern.matcher(query + "?");
while (m.find()) {
String key = m.group(1);
String value = m.group(2);
value = value.substring(0, value.length() - 1);
System.out.println(key + " = " +value);
}

Extract string between xml tags in android without parsing the xml

I have a very simple XML like this:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>
Now, I want to extract only TRUE, Hrithik Sharma, 201301-11 in 3 separate variables.
I could split the string based on the "," like this:
String[] parts = responseBody.split(",");
String response_auth = parts[0];
String user_name = parts[1];
String user_number=parts[2];
But the problem which I am facing is that, the Strings are not getting extracted independently. To be more precise, without the XML tags. How should I achieve that?
This could solve this simple case, but without parsing what are you going to do with other conditions?
public static void main(String[] args) {
String raw = "<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
raw = raw.substring(0, raw.lastIndexOf("<"));
raw = raw.substring(raw.lastIndexOf(">") + 1, raw.length());
String [] contents = raw.split(",");
for (String txt : contents)
System.out.println(txt);
}
This is highly discouraged unless you actually know what you are getting in XML
responseBody:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://icacec.com/">TRUE,Hrithik Sharma,201301-11</string>
code:
String[] parts = responseBody.split(">");
String tagsFirst= parts[0];
String usefull = parts[2];
String[] actualBody = usefull.split("<");
String content = actualBody[0];
String[] contentParts=content.split(",");
//now you can have the three parts:
String truefalse=contentParts[0];
String name=contentParts[1];
String date=contentParts[2];
Try this regex -
"<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>"
Capture groups 1, 2, and 3 will contain your three items, i.e.:
Pattern pattern = Pattern.compile("<string xmlns=\"http://icacec.com/\">(.+),(.+),(.+)</string>");
Matcher matcher = pattern.matcher("<string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>");
if(matcher.matches())
{
System.out.println("Bool: " + matcher.group(1));
System.out.println("Name: " + matcher.group(2));
System.out.println("Date: " + matcher.group(3));
}
Try to split like:
String[] strTemp = strXMLOutput.split("<TagName>");
strTemp = strTemp[1].split("</TagName>");
String strValue = strTemp[0]
100% it'll work.
You can try this:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String responseBody = null;
String inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://icacec.com/\">TRUE,Hrithik Sharma,201301-11</string>";
String regex = "<string[^>]*>(.+?)</string\\s*>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputString);
while(matcher.find()) {
responseBody = matcher.group(1);
System.out.println(responseBody);
}
String[] splits = responseBody.split(",");
System.out.println(splits[0]);/*prints TRUE*/
System.out.println(splits[1]);/*prints Hrithik Sharma*/
System.out.println(splits[2]);/*201301-11*/
}
}

Categories