Reputation: 25143
How can I remove substring between two characters(+
and @
).
Ex-
[email protected]
should give [email protected]
Which regex I should use for this.
Upvotes: 0
Views: 1063
Reputation: 11
public static string RemoveSpecialCharacters(string input) {
Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
}
Upvotes: -1
Reputation: 48610
Just write a utility class to slice Strings:
public class MyStringUtils {
public static void main (String[] args) {
String str = "[email protected]";
int startIndex = str.indexOf('+');
int endIndex = str.indexOf('@');
System.out.println("Outer: " + sliceRangeOuter(str, startIndex, endIndex));
System.out.println("Inner: " + sliceRangeInner(str, startIndex, endIndex));
}
public static String sliceStart(String str, int startIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
return str.substring(startIndex);
}
public static String sliceEnd(String str, int endIndex) {
if (endIndex < 0)
endIndex = str.length() + endIndex;
return str.substring(0, endIndex);
}
public static String sliceRangeInner(String str, int startIndex, int endIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
if (endIndex < 0)
endIndex = str.length() + endIndex;
return str.substring(startIndex, endIndex);
}
public static String sliceRangeOuter(String str, int startIndex, int endIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
if (endIndex < 0)
endIndex = str.length() + endIndex;
return sliceEnd(str, startIndex) + sliceStart(str, endIndex);
}
}
Output:
Outer: [email protected]
Inner: +12kl
Upvotes: 0
Reputation: 2603
Here is a solution using RegExp grouping.
String str = "[email protected]";
final Pattern pattern = Pattern.compile("(.+?)\\+.*@(.+?)$");
final Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1) +matcher.group(2));
Cheers
Upvotes: 0
Reputation: 75625
There is no need to use a regex. You can do it just using a for loop:
for(;;) {
int start = str.indexOf('+');
if(start == -1) break;
int stop = str.indexOf('@');
if(stop == -1) break;
str = str.substring(0,start+1) + str.substring(stop);
}
This is more verbose, but may explain better to others maintaining the code later what it is you meant to do. Not everyone is comfortable decoding regex.
Upvotes: 1
Reputation: 785156
Using regex it is:
String repl = str.replaceAll("(.*?)[+].*?(@.*)", "$1$2");
Though you can completely avoid regex and use String#indexOf
methods to find 2 positions and get substrings using that.
Upvotes: 1
Reputation: 39457
Try this one.
str = str.replaceAll("\\+[^@]*@", "@");
class Test {
public static void main(String[] args) {
String str = "bunny+12kddddd+++ddd/d/d/d/d\\####[email protected]";
str = str.replaceAll("\\+[^@]*@", "@");
System.out.println(str);
}
}
Upvotes: 2
Reputation: 328608
String s = "[email protected]";
String email = s.replaceAll("\\+.*@", "@");
Upvotes: 4