Split String using delimiter in Java - java

I need help in splitting two email address which are seperated by a Delimiter 'AND'. I have issue when splitting, when the email address has got the characters'AND' in the email id. For eg, if the email address that needs to be split is something like the below. There are no whitespaces between the two email address.
'anandc#AND.comANDxyz#yahoo.co.in', and the delimiter is'AND'
In the above case, there seems to be three items extracted instead of two. Can someone please help me solve this. Thanks in Advance

You can use " AND " as delimiter.
String str="anandc#AND.com AND xyz#yahoo.co.in";
String[] emailArr=str.split(" AND ");
Or you can use following regex
String str = "anandc#AND.com AND xyz#yahoo.co.in";
Pattern p = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9]+
(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})");
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Out put
anandc#AND.com
xyz#yahoo.co.in

Giving correct output
public class Test {
public static void main(String args[]) {
String text = "anandc#AND.com AND xyz#yahoo.co.in ";
String[] splits = text.split(" AND ");
for (int i = 0; i < splits.length; i++) {
System.out.println("data :" + splits[i]);
}
}
}
Output is
data :anandc#AND.com
data :xyz#yahoo.co.in

Use this :
String[] splits = text.split("\\s+AND\\s+");

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
the regular expression will be case sensitive
actually, the best is to use delimiters exression that you are sure will not be in the adress

Related

Get specific values from a string with regex

I try to extract string values from this string:
String str = "[{\"name:\"s2\"},{},{\"name\":\"f2\"},{\"name\":\"f2\"},{},{\"name\":\"l\"}]";
I use regex to extract "s2", "f2", "f2" and "l".
I thought about a solutions, define a regex to find string that begin with ":" + a quotation mark and end with a quotation mark.
I'm not very familiar with regex but I assumed my regex would look like something like this ? ":\".?\""
public static void main(String... args) {
Pattern p = Pattern.compile(":\".?\"");
String str = "[{\"name:\"s2\"},{},{\"name\":\"f2\"},{\"name\":\"f2\"},{},{\"name\":\"l\"}]";
Matcher m = p.matcher(str);
System.out.println(str);
while (m.find()) {
System.out.println("groupe = " + m.group());
}
}
Thanks for your help.
Use can use this pattern:
"(?<=:")[^"]*"
See Demo

JAVA Get text from String

Hi I get this String from server :
id_not="autoincrement"; id_obj="-"; id_tr="-"; id_pgo="-"; typ_not=""; tresc="Nie wystawił"; datetime="-"; lon="-"; lat="-";
I need to create a new String e.x String word and send a value which I get from String tresc="Nie wystawił"
Like #Jan suggest in comment you can use regex for example :
String str = "id_not=\"autoincrement\"; id_obj=\"-\"; id_tr=\"-\"; id_pgo=\"-\"; typ_not=\"\"; tresc=\"Nie wystawił\"; datetime=\"-\"; lon=\"-\"; lat=\"-\";";
Pattern p = Pattern.compile("tresc(.*?);");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group());
}
Output
tresc="Nie wystawił";
If you want to get only the value of tresc you can use :
Pattern p = Pattern.compile("tresc=\"(.*?)\";");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1));
}
Output
Nie wystawił
Something along the lines of
Pattern p = Pattern.compile("tresc=\"([^\"]+)\");
Matcher m = p.matcher(stringFromServer);
if(m.find()) {
String whatYouWereLookingfor = m.group(1);
}
should to the trick. JSON parsing might be much better in the long run if you need additional values
Your question is unclear but i think you get a string from server and from that string you want the string/value for tresc. You can first search for tresc in the string you get. like:
serverString.substring(serverString.indexOf("tresc") + x , serverString.length());
Here replace x with 'how much further you want to pick characters.
Read on substring and delimiters
As values are separated by semicolon so annother solution could be:
int delimiter = serverstring.indexOf(";");
//in string thus giving you the index of where it is in the string
// Now delimiter can be -1, if lets say the string had no ";" at all in it i.e. no ";" is not found.
//check and account for it.
if (delimiter != -1)
String subString= serverstring.substring(5 , iend);
Here 5 means tresc is on number five in string, so it will five you tresc part.
You can then use it anyway you want.

Check if id in string and get value if so

I am trying to get a regex to match, then get the value with it. For example, I want to check for 1234 as an id and if present, get the status (which is 0 in this case). Basically its id:status. Here is what I am trying:
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
if (topicStatus.matches(regex)) {
//How to get status?
}
Not only do I not know how to get the status without splitting and looping through, I don't know why it doesn't match the regex.
Any help would be appreciated. Thanks.
Use the Pattern class
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
Pattern MY_PATTERN = Pattern.compile(regex);
Matcher m = MY_PATTERN.matcher(topicStatus);
while (m.find()) {
String s = m.group(1);
System.out.println(s);
}
The key here is to surround the position you want [0-2] in parenthesis which means it will be saved as the first group. You then access it through group(1)
I made some assumptions that your pairs we're always comma separate and then delimited by a colon. Using that I just used split.
String[] idsToCheck = topicStatus.split(",");
for(String idPair : idsToCheck)
{
String[] idPairArray = idPair.split(":");
if(idPairArray[0].equals(someId))
{
System.out.println("id : " + idPairArray[0]);
System.out.println("status: " + idPairArray[1]);
}
}

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.

Splitting strings with multiple lines in android

I have the following string:
"ISL-1027
20:13:02:22:00:76"
i.e. bluetooth name and MAC address
I need MAC address on a separate string.
What is the best way to use split() in this case?
Thanks
split("\n") you can use this."\n" will be the separator here.
String str = "ISL-1027" +
"\n" +
"20:13:02:22:00:76";
String[] arr= str.split("\n");
System.out.println("Bluetooth Name: "+arr[0]);
System.out.println("MAC address: "+arr[1]);
Out put:
Bluetooth Name: ISL-1027
MAC address: 20:13:02:22:00:76
If your input String like this ISL-1027 20:13:02:22:00:76(separate by a space) use follows
String str = "ISL-1027 20:13:02:22:00:76";
String[] arr= str.split(" ");
System.out.println("Bluetooth Name: "+arr[0]);
System.out.println("MAC address: "+arr[1]);
Split matching on any white space and include the DOTALL mode switch:
split("(?s)\\s+");
The DOTALL will make the regex work despite the presence of newlines.
Depending on that how your string is formated is not sure, but the format of a mac-address is defined. I would not try to split the string and hope that index n is the correct value.
Instead I would use regulare expression to find the correct position of a string that matches the mac-address format.
Here is a litle example (not tested):
String input = "ISL-1027
20:13:02:22:00:76"
Pattern macAddrPattern = Pattern.compile("[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\");
String macAdr = parseMacAddr(input);
public String parseMacAddr(String value) {
Matcher m = macAddrPattern.matcher(value);
if (m.matches()) {
return value.substring(m.start(),m.end());
}
return null;
}
This should always work.

Categories

Resources