Regular expression to remove specific characters in email addresses - java

Im trying to figure out how i can remove certain characters in an email address before the domain name using nothing but a simple regex and replaceAll in Java.
In email addresses,
Need to remove any number of . before #<domain name>
Also remove anything between + up to # but not including #. For instance in joebloggs+123#domain.com should be joebloggs#domain.com.
So far I have,
class Main {
public static void main(String[] args) {
String matchingRegex = "(\\.|(\\+.*(?=#)))";
System.out.println("joe.bloggs+123#gmail.com".replaceAll(matchingRegex, ""));
}
}
which replaces everything including the domain name.
joebloggs#gmailcom
What i really need is joebloggs#gmail.com.
Can this be achieved with regex alone ?

Another look ahead did the trick in the end.
class Main {
public static void main(String[] args) {
String matchingRegex = "((\\.+)(?=.*#)|(\\+.*(?=#)))";
System.out.println("joe.bloggs+123#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joebloggs+123#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joe.bloggs#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joe.bloggs.123#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joe.bloggs.123+456#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joebloggs#gmail.com".replaceAll(matchingRegex, ""));
System.out.println("joe.bloggs.123+456.789#gmail.com".replaceAll(matchingRegex, ""));
}
}
Results in,
joebloggs#gmail.com
joebloggs#gmail.com
joebloggs#gmail.com
joebloggs123#gmail.com
joebloggs123#gmail.com
joebloggs#gmail.com
joebloggs123#gmail.com

You could try spliting the string (the email) on the # and running replaceAll on the the first half and then put the strings back together.
Check out: How to split a string in Java
For splitting strings.

Try this regex [.](?=.*#)|(?=\\+)(.*)(?=#). It looks up dots up to # (even if there's text in between), or everything from + up to #. Hope it helps https://regex101.com/r/gyUpta/1
class Main {
public static void main(String[] args) {
String matchingRegex = "[.](?=.*#)|(?=\\+)(.*)(?=#)";
System.out.println("joe.bloggs+123#gmail.com".replaceAll(matchingRegex, ""));
}
}

This will do the trick...
public static void main(String args[]) {
String matchingRegex = "(\\.|(\\+.*(?=#)))";
String email = "joe.bloggs+123#gmail.com";
String user = email.substring(0, email.indexOf("#")+1);
String domain = email.substring(email.indexOf("#")+1);
System.out.println(user.replaceAll(matchingRegex, "") + domain);
}

This is the easiest way I have found to do it.
String address = "joe.bloggs+123#gmail.com";
int at = address.indexOf("#");
address = address.substring(0, at).replaceAll("\\.|\\+.*", "")
+ address.substring(at);
System.out.println(address);

if you try to split for regex sorry i don't remember java this example is in javascript
let string = "joe.bloggs+123#gmail.com"
//firts the function
function splitString(params) {
return params.split(/\+(.)+\#/)
}
//second the concat
let list = splitString(string)
// the first element+the las element
console.log(`${list[0]}${list[list.length -1]}`)

Related

Regex max a string till " and not stop at \"

I have a String to be checked for regex :
"field":"Testing, for something \"and something\""
which I want to pattern match and replace with :
"field":"SAFE"
For this, I am trying to pattern match and capture till the last inverted commas. I have tried the following regex, but its not matching :
Pattern p = Pattern.compile("\"field\":\".*?(?!\\\")\"");
New to regex, can anyone suggest what I might be doing wrong? Thanks!
EDIT :
I guess the question was not clear. Apologies. The above is not the end of the string. It can contain more fields in succession :
"field":"Testing, for something \"and something\"", "new_field":"blahblah", ...
output should be :
"field":"SAFE", "new_field":"blahblah", ...
You can do it as follows:
public class Testing {
public static void main(String[] args) {
String str = "\"field\":\"Testing, for something \\\"and something\\\"\"";
str = str.replaceAll("(\"field\":).*", "$1\"SAFE\"");
System.out.println(str);
}
}
Output:
"field":"SAFE"
Explanation:
(\"field\":) is the first capturing group
.* specifies all characters
$1 specifies the first capturing group
Update:
Writing this update based on the clarification from OP.
You can use positive lookahead for comma as shown below:
public class Testing {
public static void main(String[] args) {
String str = "\"field\":\"Testing, for something \\\"and something\\\"\", \"new_field\":\"blahblah\"";
str = str.replaceAll("(\"field\":).*(?=,)", "$1\"SAFE\"");
System.out.println(str);
}
}
Output:
"field":"SAFE", "new_field":"blahblah"
Here is an example.
$str = '"field":"Testing, for something \"and something\""';
echo preg_replace('/(\"field\":\")(.*)(\")/i', "$1SAFE$3", $str);
Regex is tested: here.

Android Detect space in given String and break line

Just a quick question, as after an hour of research I haven't yet found a solution to my problem:
I have a given String (from online DB) that may contain spaces and I need to break the line at every space.
The output must be a String
Could someone help please?
Cheers
String newString = oldString.replaceAll(" ","\n");
You can do it like this for e.g.
String s = I am a dragon.
s.replaceAll(" ", "\n");
You can split It
class A{
public static void main(String[] args) {
String word = "Hello Hello Hello Hello ";// Now Your String like This
String arr[] = word.split(" ");// You can Split it from every Space
for (String name : arr) {
System.out.println(name);// output is a String
}
}
}

How to compare two Strings in java without considering spaces?

I have one example.
public class Test {
public static void main(String[] args) {
String a="VIJAY KAKADE";
String b="VIJAY KAKADE";
if(a.equalsIgnoreCase(b)){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
I need to check these strings without considering spaces. How do I achieve this? How do I ignore spaces in the strings when I compare them?
You can try to create a new string by replacing all empty spaces.
if(a.replaceAll("\\s+","").equalsIgnoreCase(b.replaceAll("\\s+",""))) {
// this will also take care of spaces like tabs etc.
}
then compare.
I think replacing all spaces with an empty string poses the danger of verifying the following situation (finding the two names equal):
String a = "ANN MARIE O'RIORDAN"
String b = "ANNMARIE O'RIORDAN"
I know I may be splitting hairs here, but I found this question while looking for a similar solution to verify SQL queries in a unit test. Because my queries are multi-line static final Strings, I wanted to make sure that I didn't miss a space anywhere.
To that end, I think replacing all whitespaces with a single space, or perhaps a special character is the safest solution - which then does require regex:
if (a.trim().replaceAll("\\s+", " ").equalsIgnoreCase(b.trim().replaceAll("\\s+", " "))) {
// Strings equivalent
}
Thoughts?
As Zoltan correctly pointing out, all answers besides his are in fact wrong.
For using the functionality from a third party library I suggest hamcrest:
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace;
public class Main {
public static void main(String[] args) {
String a = "VIJAY KAKADE";
String b = "VIJAY KAKADE";
System.out.print(String.format("'%s' and '%s' matching: ", a, b));
if (equalToIgnoringWhiteSpace(a).matches(b)) {
System.out.println("yes");
} else {
System.out.println("no");
}
String c = "VIJAYKAKADE";
System.out.print(String.format("'%s' and '%s' matching: ", a, c));
if (equalToIgnoringWhiteSpace(a).matches(c)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
returns:
'VIJAY KAKADE' and 'VIJAY KAKADE' matching: yes
'VIJAY KAKADE' and 'VIJAYKAKADE' matching: no
Replace the spaces with empty string:
if (a.replace(" ", "").equalsIgnoreCase(b.replace(" ", "")))
if you want to replace all whitespace, including tabs etc, you can use
a = yourOriginalString.replaceAll("\\s", "");
b = yourOriginalString.replaceAll("\\s", "");
return a.equalsIgnoreCase(b);
edit: woah ninja'd like heck
You can use String.replace() to remove the spaces in both strings.
String aMod = a.replace(" ","");
String bMod = b.replace(" ","");
if( aMod.equalsIgnoreCase(bMod) ){
...
String#replace() method is helpful for you.
public static void main(String[] args) {
String a="VIJAY KAKADE";
String b="VIJAY KAKADE";
a = a.replace(" ", "");
b = b.replace(" ", "");
if(a.equalsIgnoreCase(b)){
System.out.println("yes");
}else{
System.out.println("no");
}
}
a.replace(" ","")
is your best bet. However you can use
a.trim()
to remove leading and trailing whitespaces if you know want to ignore only the leading and trailing whitespaces. Also the StringUtils from apache commons has many more functions to help
public static void main(String args[]) {
String a = "My Name is A B";
String b = "My Name is A B";
System.out.println("Match = " + equalsIgnoreSpace(a, b, false));
}
static boolean equalsIgnoreSpace(String s1, String s2, boolean matchCase) {
String val1 = stripExtraSpaces(s1);
String val2 = stripExtraSpaces(s2);
if(matchCase) {
return val1.equals(val2);
} else {
return val1.equalsIgnoreCase(val2);
}
}
static String stripExtraSpaces(String s) {
String formattedString = "";
java.util.StringTokenizer st = new java.util.StringTokenizer(s);
while(st.hasMoreTokens()) {
formattedString += st.nextToken() + " ";
}
return formattedString.trim();
}

Issue with regex

I want to write a regex where a string has
(9 characters) and start with either "g" or "r"
and then are all numbers afterward.
I have written this but it does not work:
public static void main(String[] args) {
String id= "g57895452";
String pattern = "/^g([0-9]+){8}$/";
if (id.matches(pattern)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
Corrected re:
"^[gr]([0-9]{8})$"
You need not + when you already has {8}.
Also you don't need () when you don't want to use the group further in the code.
"^[gr][0-9]{8}$"
Remove the / from start and end of your regex pattern, It will work.

How can I remove any caret symbols in a string?

I'm using a regex string that contains a carat (^) symbol somewhere inside of it. Is there a way in Java to remove these symbols? Here are a few methods I've tried:
string = "some^string";
string = string.replaceAll("\\^", "");
string = string.replaceAll(Pattern.quote("\\^"), "");
string = string.replaceAll(Pattern.quote("\u2038"), "");
None of which have worked. What am I missing?
There's no need to use regular expressions at all:
string = string.replace("^", "");
However, the first of your examples works too:
public class Test {
public static void main(String[] args) throws Exception {
String string = "some^string";
string = string.replaceAll("\\^", "");
System.out.println(string); // Prints somestring
}
}
... so it's entirely possible that your problem is elsewhere.
string.replaceAll("\\^", ""); should work.
Pls. delete last 2 lines from above your code and do write below things only then check.
string = "some^string";
string = string.replaceAll("\^", "");
I think you can try using another variable name, like for example:
String str1 = string.replace("^", "1");
and use the new str1, instead of the old string.

Categories