I have following example string that needs to be filtered
0173556677 (Alice), 017545454 (Bob)
This is how phone numbers are added to a text view. I want the text to look like that
0173556677;017545454
Is there a way to change the text using regular expression. How would such an expression look like? Or do you recommend an other method?
You can do as follows:
String orig = "0173556677 (Alice), 017545454 (Bob)";
String regex = " \\(.+?\\)";
String res = orig.replaceAll(regex, "").replaceAll(",", ";");
// ^remove all content in parenthesis
// ^ replace comma with semicolon
Use the expression in android.util.Patterns
Access the static variable
Patterns.PHONE
or use this expression here (Android Source Code)
Here's a resource that can guide you :
http://www.zparacha.com/validate-email-ssn-phone-number-using-java-regular-expression/
This solution works with phone numbers separated with any string that does not contain numbers:
String orig = "0173556677 (Alice), 017545454 (Bob)";
String[] numbers = orig.split("\\D+"); //split at everything that is not a digit
StringBuilder sb = new StringBuilder();
if (numbers.length > 0) {
sb.append(numbers[0]);
for (int i = 1; i < numbers.length; i++) { //concatenate all that is left
sb.append(";");
sb.append(numbers[i]);
}
}
String res = sb.toString();
or, with com.google.common.base.Joiner:
String[] numbers = orig.split("\\D+"); //split at everything that is not a digit
String res = Joiner.on(";").join(numbers);
PS. There is a minor deviation from the requirements in the best voted example, but it seems I cannot just add one character (should be replaceAll(", ", ";"), with a space after the coma, or a \\s) and I do not want to mess somebody's code.
Related
Changing string with comma separated values to numbered new-line values
For example:
Input: a,b,c
Output:
1.a
2.b
3.c
Finding it hard to change it using regex pattern, instead of converting string to string array and looping through.
I'm not really sure, that it's possible to achive with only regex without any kind of a loop. As fore me, the solution with spliting the string into an array and iterating over it, is the most straightforward:
String value = "a,b,c";
String[] values = value.split(",");
String result = "";
for (int i=1; i<=values.length; i++) {
result += i + "." + values[i-1] + "\n";
}
Sure, it's possible to do without splitting and any kind of arrays, but it could be a little bit awkward solution, like:
String value = "a,b,c";
Pattern pattern = Pattern.compile("[(^\\w+)]");
Matcher matcher = pattern.matcher(value.replaceAll("\\,", "\n"));
StringBuffer s = new StringBuffer();
int i = 0;
while (matcher.find()) {
matcher.appendReplacement(s, ++i + "." + matcher.group());
}
System.out.println(s.toString());
Here the , sign is replaced with \n new line symbol and then we are looking for a groups of characters at the start of every line [(^\\w+)]. If any group is found, then we are appending to the start of this group a line number. But even here we have to use a loop to set the line number. And this logic is not as clear, as the first one.
I need to split a string based on delimiters and assign it to an object. I am aware of the split function, but I am unable to figure how to do it for my particular string.
The object is of the format:
class Selections{
int n;
ArrayList<Integer> choices;
}
The string is of the form :
1:[1,3,2],2:[1],3:[4,3],4:[4,3]
where:
1:[1,3,2] is an object with n=1 and Arraylist should have numbers 1,2,3.
2:[1] is an object with n=2 and Arraylist should have number 1
and so on .
I cannot use split with "," as delimiter because both individual objects and the elements within [] are separated by ",".
Any ideas would be appreciated.
You could use a regex to have a more robust result as follows:
String s = "1:[1,3,2],2:[1],3:[4,3],4:[4,3],5:[123,53,1231],123:[54,98,434]";
// commented one handles white spaces correctly
//Pattern p = Pattern.compile("[\\d]*\\s*:\\s*\\[((\\d*)(\\s*|\\s*,\\s*))*\\]");
Pattern p = Pattern.compile("[\\d]*:\\[((\\d*)(|,))*\\]");
Matcher matcher = p.matcher(s);
while (matcher.find())
System.out.println(matcher.group());
The regex can probably be tuned to be more accurate (e.g., handling white spaces) but it works fine on the example.
How about using "]," as delimiter?
If your structure is strictly like you said, it should be able to identify and split.
(Sorry, I want to leave it as comment, but my reputation does not allow)
You will need to perform multiple splits.
Split with the delimiter "]," (as mentioned in other comments and answers).
For each of the resulting strings, split with the delimiter ":[".
you will need to cleanup the last entry (from the split in step 1), because it will end with ']'
I have no idea how to use a build-in function for this. I would just write my own split method:
private List<Sections> split(String s){
private List<Sections> sections = new ArrayList<>();
private boolean insideBracket = false;
private int n = 0;
private List<Integer> ints = new ArrayList<>();
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(!insideBracket && !c.equals(':')){
n = c.getNumericValue();
} else if(c.equals('[')){
insideBracket = true;
} else if (c.equals(']')){
insideBracket = false;
sections.add(new Section(n, ints));
ints = new ArrayList();
} else if(insideBracket && !c.equals(',')){
ints.add(c.getNumericValue());
}
}
}
you probably need to modify that a little bit. Right now it dont works if a number has multiple digits.
Try this
while(true){
int tmp=str.indexOf("]")+1;
System.out.println(str.substring(0,tmp));
if(tmp==str.length())
break;
str=str.substring(tmp+1);
}
I am trying to get a sentence using input from the user in Java, and i need to make it lowercase and remove all punctuation. Here is my code:
String[] words = instring.split("\\s+");
for (int i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}
String[] wordsout = new String[50];
Arrays.fill(wordsout,"");
int e = 0;
for (int i = 0; i < words.length; i++) {
if (words[i] != "") {
wordsout[e] = words[e];
wordsout[e] = wordsout[e].replaceAll(" ", "");
e++;
}
}
return wordsout;
I cant seem to find any way to remove all non-letter characters. I have tried using regexes and iterators with no luck. Thanks for any help.
This first removes all non-letter characters, folds to lowercase, then splits the input, doing all the work in a single line:
String[] words = instring.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
Spaces are initially left in the input so the split will still work.
By removing the rubbish characters before splitting, you avoid having to loop through the elements.
You can use following regular expression construct
Punctuation: One of !"#$%&'()*+,-./:;<=>?#[]^_`{|}~
inputString.replaceAll("\\p{Punct}", "");
You may try this:-
Scanner scan = new Scanner(System.in);
System.out.println("Type a sentence and press enter.");
String input = scan.nextLine();
String strippedInput = input.replaceAll("\\W", "");
System.out.println("Your string: " + strippedInput);
[^\w] matches a non-word character, so the above regular expression will match and remove all non-word characters.
If you don't want to use RegEx (which seems highly unnecessary given your problem), perhaps you should try something like this:
public String modified(final String input){
final StringBuilder builder = new StringBuilder();
for(final char c : input.toCharArray())
if(Character.isLetterOrDigit(c))
builder.append(Character.isLowerCase(c) ? c : Character.toLowerCase(c));
return builder.toString();
}
It loops through the underlying char[] in the String and only appends the char if it is a letter or digit (filtering out all symbols, which I am assuming is what you are trying to accomplish) and then appends the lower case version of the char.
I don't like to use regex, so here is another simple solution.
public String removePunctuations(String s) {
String res = "";
for (Character c : s.toCharArray()) {
if(Character.isLetterOrDigit(c))
res += c;
}
return res;
}
Note: This will include both Letters and Digits
If your goal is to REMOVE punctuation, then refer to the above. If the goal is to find words, none of the above solutions does that.
INPUT: "This. and:that. with'the-other".
OUTPUT: ["This", "and", "that", "with", "the", "other"]
but what most of these "replaceAll" solutions is actually giving you is:
OUTPUT: ["This", "andthat", "withtheother"]
I'm trying to remove a specific word from a certain string using the function replace() or replaceAll() but these remove all the occurrences of this word even if it's part of another word!
Example:
String content = "is not like is, but mistakes are common";
content = content.replace("is", "");
output: "not like , but mtakes are common"
desired output: "not like , but mistakes are common"
How can I substitute only whole words from a string?
What the heck,
String regex = "\\s*\\bis\\b\\s*";
content = content.replaceAll(regex, "");
Remember you need to use replaceAll(...) to use regular expressions, not replace(...)
\\b gives you the word boundaries
\\s* sops up any white space on either side of the word being removed (if you want to remove this too).
content = content.replaceAll("\\Wis\\W|^is\\W|\\Wis$", "");
You can try replacing " is " by " ". The is with a space before and one after, replaced by a single space.
Update:
To make it work for the first "is" in the sentence, also do another replace of "is " for "". Replacing the first is and the first space, with an empty string.
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
char c = s.next().charAt(0);
System.out.println(removeAllOccurrencesOfChar(input, c));
}
public static String removeAllOccurrencesOfChar(String input, char c) {
String r = "";
for (int i = 0; i < input.length(); i ++) {
if (input.charAt(i) != c) r += input.charAt(i);
}
return r;
}
}
I'm using java to try to replace the four spaces at the beginning of a new line to a tab. I would like to do this using regular expressions. My issue is that the regex is currently replacing all four-space-sequences with a single tab. I want it to insert a tab for each four-space-sequence. Right now I have:
public String translate(String text) {
text = text.replaceAll("(?m)^( )+", "\t");
return text;
}
You don't want the + because you want exactly 4 spaces, and you need a look-behind assertion:
text = text.replaceAll("(?m)(?<=^ *) ", "\t");
Note: This should only be used for input of about 10K or less due to the backtracking required by the look behind. For larger input, use a pattern and matcher etc
Unlike the other answers, this one actually works (see test below), because it uses a positive look-behind (?<=^ *) to assert that only spaces are between the start of input and the target replacement, without which you'll only match the first 4 spaces:
String text = " a\n b \n";
text = text.replaceAll("(?m)(?<=^ *) ", "\t");
System.out.println(text.replace("\t", "TAB"));
Output:
TABTABa
TABTABb
Is it really necessary to use regular expressions for that? If not, i suggest the following code. It will replace every 4-spaces sequence at the line start with a tab (i.e. one tab per sequence), and won't affect all other 4-spaces.
Unlike regular expressions, this code performs instantly even for large text block.
String text = " 4 spaces\n four more\n \n text";
String[] split = text.split("\n");
StringBuilder result = new StringBuilder(text.length());
for (String string : split) {
int idx = 0;
while (true) {
String temp = string.substring(idx);
if (temp.startsWith(" ")) {
// Append a tab and push actual start index further
result.append('\t');
idx = idx + 4;
} else {
result.append(temp);
break;
}
}
result.append('\n');
}
System.out.println(text);
System.out.println("====");
System.out.println(result);