String Truncation in java - java

I have a string like
String email = "mailto://abc#gmail.com";
I want to get only the email address but without using a fixed number like
email.substring(9);
Any better approach.

The String is of the URI format so you could do
String email = "mailto://abc#gmail.com";
URI uri = URI.create(email);
String address = uri.getUserInfo() + "#" + uri.getHost();

Use a regular expression:
String email = "mailto://abc#gmail.com";
// Builds a pattern with a capturing group ()
Pattern mailtoPattern = Pattern.compile("mailto://(.*)");
// Give your string to a matcher generated by the compiled pattern
Matcher mailMatcher = mailtoPattern.matcher(email);
// If your String is correctly formatted you can attempt to capture
// the content between parenthesis
if (mailMatcher.find()) {
String mailValue = emailMatcher.group(1);
}
Using regular expressions will also help you validate the String given as input, you can even validate if the mail String is indeed a mail address (there are crazy people with all sorts of crazy expressions to validate them). I recommend you read the very thorough JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html.

Not using regex
String string = "mailto://abc#gmail.com";
final String PREFIX = "mailto://";
String email = string.substring(PREFIX.length());

Related

How to navigate to correct "URL" from outlook email content

I'm successfully reading outlook email from JAVAX mail. But when i try to get the "Link" available in email body it's not giving the exact URL, instead it gives the URL with some extra characters like "=3D?*/". I tried to use below code but it didn't help me.
public List<String> getUrlsFromMessage(Message message, String linkText) throws Exception {
String html = getMessageContent(message);
List<String> allMatches = new ArrayList<String>();
// (<a [^>]+>)
Matcher matcher = Pattern.compile(" (<a [^>]+>)" + linkText + "</a>").matcher(html);
while (matcher.find()) {
String aTag = matcher.group(1);
allMatches.add(aTag.substring(aTag.indexOf("http"), aTag.indexOf("\">")));
}
return allMatches;
}
Also I changed the pattern to
Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"([^\"]*)[^>]*>(.*?)</a>",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);`
But still it gives me the wrong URL.
Finally I found a solution to retrieve the exact URL using StringBuilder. What i did was i removed the unwanted characters from the string until i get the correct URL. This may not be a good coding practice but this was the only work around which works for me.
StringBuilder build = new StringBuilder(link);
build.deleteCharAt(43);// Shift the positions front.
build.deleteCharAt(51);
build.deleteCharAt(51);
driver.get(build.toString());

Java Regex multi delimiter split in order

I am trying to split a string having multi-delimiters in it but want to first check if the string satisfies the regex and then split based on it.
Example:-
The testString will contain ://,:,#,:,/ these characters in specific order and I need to first check if the given string satisfies the pattern or not and if satisfy then split it. The string other characters can also contain these in it but I need to split based on order of these ://,:,#,:,/
String testString = "aman://jaspreet:raman!#127.0.0.1:5031/test";
String[]tokens = testString.split("://|\\:|#|\\:|\\/");
for(String s:tokens) {
System.out.println(s);
}
Here above I have tried the regex to split but it doesn't split by checking in order. It just checks if any given regex character exists in string or not and then splits based on it.
If you first validate the pattern, then you shouldn't do split() after. Use capturing groups to gather the data you already validated.
E.g. in a simple case, foo#bar, with separator #, you would validate with ^([^#]+)#(.+)$, i.e. match and capture text up to #, match but don't capture the #, then match and capture the rest:
Pattern p = Pattern.compile("^([^#]+)#(.+)$");
Matcher m = p.matcher("foo#bar");
if (! m.matches()) {
// invalid data
} else {
String a = m.group(1); // a = "foo"
String b = m.group(2); // b = "bar"
// use a and b here
}
For the matching in the question, a lenient pattern could be:
^(.*?)://(.*?):(.*?)#(.*?):(.*?)/(.*)$
You would then use code above, but with:
String scheme = m.group(1); // "aman"
String user = m.group(2); // "jaspreet"
String password = m.group(3); // "raman!"
String host = m.group(4); // "127.0.0.1"
String port = m.group(5); // "5031"
String path = m.group(6); // "test"
For a stricter matching, replace .*? with a pattern that only matches allowed characters, e.g. [^:]+ if it cannot be empty and cannot contain colons.
Alternatively, you could just use the URI class to parse the URL string.
String testString = "aman://jaspreet:raman!#127.0.0.1:5031/test";
URI uri = URI.create(testString);
String scheme = uri.getScheme(); // "aman"
String userInfo = uri.getUserInfo(); // "jaspreet:raman!"
String host = uri.getHost(); // "127.0.0.1"
String port = uri.getPort(); // "5031"
String path = uri.getPath(); // "test"

How to get the string from matcher.group() and store it in separate string variable in java?

String emailAdress = "yourname#yourdomin.com";
Pattern emailAddress = Pattern.compile("(.*)(#)(.*)");
Matcher matchEmailAddress = emailAddress.matcher(emailAdress);
String secondPartOfEmail;
while(matchEmailAddress.find()){
System.out.println(matchEmailAddress.group(1));
System.out.println(matchEmailAddress.group(3));
}
When I run this source code, the output is:
yourname yourdomin.com
I want to store yourdomain.com in string type variable to use it later. I mean group(3) in matchEmailAddress matcher.
I've already tried:
String secondPartOfEmail = matchEmailAddress.group(3)
but a error occured.
Assuming you want to match only one email address, you can do this:
String emailAdress = "yourname#yourdomin.com";
Pattern emailAddress = Pattern.compile("(.*)(#)(.*)");
Matcher matchEmailAddress = emailAddress.matcher(emailAdress);
matchEmailAddress.find(); //find the next substring matching your pattern
String secondPartOfEmail = matchEmailAddress.group(3);

How to remove specific character from a string?

I have a string that saves user login name and I want to remove specific characters from that string,i want to remove "#gmail.com" and just have the name before the #, then save it as a new string?
How can I do this?
Here's an example, email can be any email address, not just gmail.com
public class Test {
public static void main(String[] args) {
String email = "nobody#gmail.com";
String nameOnly = email.substring(0,email.indexOf('#'));
System.out.println(nameOnly);
}
}
make sure the email format be correct then use "split" method to split the string from '#' character's position and use first portion of results.
var str = "username#amailserver.com";
var res = str.split("#");
var username = res[0];
You can use regex + replaceAll method of string for eliminate it
sample:
String s = "Rod_Algonquin#company.co.nz";
String newS = s.replaceAll("#(.*).(.*)", "");
System.out.println(newS);
will work on different sites extension.
if you want .org, .net , etc then you need to change the regex #(.*).(.*)

How to filter a String in java with specific range (email format)

I have a String in Java called Kiran<kiran#gmail.com>. I want to get String just kiran#gmail.com by removing other content.
String s1= kiran<kiran#gmail.com>
Output should be kiran#gmail.com
Please help me out in solving this.
If you are trying to parse email addresses, I'd recommend to use the InternetAddress class. It is part of Java EE (if you are using Java SE you need to add the javax.mail dependency).
That class is able to parse an String containing an email address like yours.
String s1 = "kiran<kiran#gmail.com>";
InternetAddress address = new InternetAddress(s1);
String email = address.getAddress();
I think this way:
Your algorithm is automatically standards-compliant
Your code is cleaner than using regular expressions to extract the email address.
You can do the following thing.
String s = "To: John Smith <john#smith.com>, Janes Smith\n"
+ "<jane#smith.org>, Tom Barter <tom#test.co.uk>, Other \n"
+ "Weird ##$#<>#^Names <other#names.me>, \n"
+ "Long Long Long Long Name <longlong#name.com>";
s = s.substring(3); // filter TO:
System.out.println(s);
// Use DOTALL pattern
Pattern p = Pattern.compile("(.*?)<([^>]+)>\\s*,?",Pattern.DOTALL);
Matcher m = p.matcher(s);
while(m.find()) {
// filter newline
String name = m.group(1).replaceAll("[\\n\\r]+", "");
String email = m.group(2).replaceAll("[\\n\\r]+", "");
System.out.println(name + " -> " + email);
}

Categories