Regular expression number - java

My target is to format next number 01122222222 to this 011-22-22-22-22
First three numbers of number, dash and all next numbers with dash after second one. Already tried:
private String phoneFormat(String phoneNumber){
String regex = "\\B(?=(\\d{3})+(?!\\d))";
String formattedPhone = phoneNumber.replaceAll(regex, Constants.Characters.DASH);
return formattedPhone;
}
but it produce different result.

A regex will do the trick. Replace sets of 2 digits with "-[digit][digit]" as long as you have 3 digits before those.
public static void main(String[] args) {
String s = "01122222222";
System.out.println(s.replaceAll("(?<=\\d{3})(\\d{2})+?", "-$1"));
}
Live Example
O/P :
011-22-22-22-22
PS : This approach should NOT be used in prod environment and has been written solely to please my own stubbornness that this problem can be solved using one regex.

Since at first your question wasn't clear, I had a solution for both Java and Javascript. I'll leave the javascript one in here as well just 'cause :-)
Java 8
First we use substring(0,3) to get the first three numbers. We add - to this, and with the remaining group (x.substring(3)), we split them in groups of two, and we join them together with String.join and use - as the concatenating character.
String test = "01122222222";
String res = test.substring(0,3) + "-";
String[] parts = test.substring(3).split("(?=(?:..)*$)");
res += String.join("-",parts);
System.out.println(res);
Live Example
Pre Java 8
From the comments it became clear that you are not using Java 8, so pre-java8 there are various other solutions. You could use a loop like I have done, and add the last part manually. (Alternatively, you could just create the string with each element in the loop, and take the substring again to remove the last -).
String test = "01122222222";
String res = test.substring(0,3) + "-";
String[] parts = test.substring(3).split("(?=(?:..)*$)");
for(int i = 0; i < parts.length-1; i++){
res += parts[i]+"-";
}
res+=parts[parts.length-1];
System.out.println(res);
Live Example
Javascript
Using the same logic, you could do this in javascript as well. You can run the snippet to see that the result is actually what you expected.
var x = "01122222222";
var res = x.substring(0,3) +"-"+ x.substring(3).split(/(?=(?:..)*$)/).join("-");
console.log(res)

Related

How to split a string by a newline and a fixed number of tabs like "\n\t" in Java?

My input string is the following:
String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
My intended result is
dir,
subdir1,
subdir2\n\t\tfile.ext
The requirement is to split the input by "\n\t" but not "\n\t\t".
A simple try of
String[] answers = input.split("\n\t");
also splits "\tfile.ext" from the last entry. Is there a simple regular expression to solve the problem? Thanks!
You can split on a newline and tab, and assert not a tab after it to the right.
\n\t(?!\t)
See a regex demo.
String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] answers = input.split("\\n\\t(?!\\t)");
System.out.println(Arrays.toString(answers));
Output
[dir, subdir1, subdir2
file.ext]
If you are looking for a generic approach, it highly depends on what format will input generally have. If your format is static for all possible inputs (dir\n\tdir2\n\tdir3\n\t\tfile.something) one way to do it is the following:
String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] answers = input.split("\n\t");
for (int i = 1; i < answers.length; i++)
if (answers[i].contains("\t"))
answers[i-1] = answers[i-1] + "\n\t" + answers[i];
String[] answersFinal = Arrays.copyOf(answers, answers.length-1);
for (int i = 0; i < answersFinal.length; i++)
answersFinal[i] = answers[i];
for (String s : answersFinal)
System.out.println(s);
However this is not a good solution and I would suggest reformatting your input to include a special sequence of characters that you can use to split the input, for example:
String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
input = input.replaceAll("\n\t", "%%%").replaceAll("%%%\t", "\n\t\t");
And then split the input with '%%%', you will get your desired output.
But again, this highly depends on how generic you want it to be, the best solution is to use an overall different approach to achieve what you want, but I cannot provide it since I don't have enough information on what you are developing.
You can simply do:
String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] modifiedInput = input.replaceAll("\n\t\t", "####").replaceAll("\n\t", "§§§§").replaceAll("####", "\n\t\t").split("§§§§");
Replace each \n\t\t which contain the \n\t
Replace each \n\t
Change back the \n\t\t as you seemingly want to preserve it
Make the split.
Not very efficient but still works fast enough if you won't use it in mass data situations.
This approach is more efficient as it only uses 2 splits but only works if there is only one element prefixed with \n\t\t at the end. Accessing an Array is kind of cheap O(1) so constant time. More code but less full iterations (replaceAll, split).
final String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
final String[] s1 = input.split("\n\t\t");
final String last = s1[s1.length - 1];
final String[] modifiedInput = s1[0].split("\n\t");
modifiedInput[modifiedInput.length -1] = modifiedInput[modifiedInput.length -1] + "\n\t\t" + last;

How to remove a trailing comma from a string (Java)

I have an array, which I use a for each loop to iterate through. I then store it in a string and then try to add a comma to it, but am struggling look for a "logic" based solution.
I am almost close to about 1 year's worth of Java under my belt, so most of the solutions I am trying to find to implement are mostly logic based (for now), since this is apart of Java MOOC's course. What are some options I can look at? What am I missing?
for(int number: array){
String thread = "";
thread += number + ", ";
System.out.print(thread);
}
You can use a Stream to achieve this result.
System.out.println(Arrays.stream(array).collect(Collectors.joining(",")));
I'm not sure the constraints of this project for your course, but if you're able, try a StringJoiner! It will add a delimiter automatically to separate items that are added to it. Another note, I think you're going to want to declare your String outside of your for loop. otherwise it resets every iteration.
StringJoiner joiner = new StringJoiner(",");
for(int number : array){
joiner.add(String.valueOf(number));
}
System.out.print(joiner.toString());
What I like to do when I'm just doing something simple and quick is this:
String thread = "";
for (int i = 0; i < array.length; i++) {
int number = array[i];
thread += number;
if (i < array.length - 1) {
thread += ", ";
}
}
Basically all it does is check that we aren't on the last index and append the comma only if it isn't the last index. It's quick, simple, and doesn't require any other classes.
Pressuming you had a string ending with a comma, followed by zero or more white spaces you could do the following. String.replaceAll() uses a regular expression to detect the replacement part.
\\s* means 0 or more white spaces
$ means at the end of the line
String str = "a, a, b,c, ";
str = str.replaceAll(",\\s*$","");
Prints
a, a, b,c

Splitting string in between two characters in Java

I am currently attempting to interpret some code I wrote for something. The information I would like to split looks something like this:
{hey=yes}TEST
What I am trying to accomplish, is splitting above string in between '}' and 'T' (T, which could be any letter). The result I am after is (in pseudocode):
["{hey=yes}", "TEST"]
How would one go about doing so? I know basic regex, but have never gotten into using it to split strings in between letters before.
Update:
In order to split the string I am using the String.split method. Do tell if there is a better way to go about doing this.
You can use String's split method, as follow:
String str = "{hey=foo}TEST";
String[] split = str.split("(?<=})");
System.out.println(split[0] + ", " + split[1]);
It splits the string and prints this:
{hey=foo}, TEST
?<=}, is to split after the character } and keep the character while doing it. By default, if you just split on a character, it will be removed by the split.
This other answer provides a complete explanation of all options when using the split method:
how-to-split-string-with-some-separator-but-without-removing-that-separator-in-j
Usage of regexp for such a small piece of code can be really slow, if it is repeated thousands of times (e.g. like analysing Alfresco metadata for lot of documents).
Look at this snippet:
String s = "{key=value}SOMETEXT";
String[] e = null;
long now = 0L;
now = new Date().getTime();
for (int i = 0; i < 3000000; i++) {
e = s.split("(?<=})");
}
System.out.println("Regexp: " + (new Date().getTime() - now));
now = new Date().getTime();
for (int i = 0; i < 3000000; i++) {
int idx = s.indexOf('}') + 1;
e = new String[] { s.substring(0, idx), s.substring(idx) };
}
System.out.println("IndexOf:" + (new Date().getTime() - now));
result is
Regexp: 2544
IndexOf:113
This means that regexp is 25 times slower than a (easier) substring. Keep it in mind: it can make the difference between a efficient code and a elegant (!) one.
If you're looking for a regex approach and also want some validation that input follows the expected syntax you probably want something like this:
public List<String> splitWithRegexp(String string)
{
Matcher matcher = Pattern.compile("(\\{.*\\})(.*)").matcher(string);
if (matcher.find())
return Arrays.asList(matcher.group(1), matcher.group(2));
else
throw new IllegalArgumentException("Input didn't match!");
}
The parenthesis in the regexp captures groups, which you can access with matcher.group(n) calls. Group 0 matches the whole pattern.

Java Split String Using Delimiter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Matching a “.” in java
I have a String 1.2.4 which i want to split and get 2 to compare it with another String that i get.
I am trying to do the following
StringTokenizer old_ver = new StringTokenizer(oldVersion, ".");
int oldVer = Integer.parseInt(old_ver.nextToken());
oldVer = Integer.parseInt(old_ver.nextToken());
StringTokenizer new_ver = new StringTokenizer(newVersion, ".");
int newVer = Integer.parseInt(new_ver.nextToken());
newVer = Integer.parseInt(new_ver.nextToken());
if (oldVer == newVer) {
return true;
}
IS there a better way to do it using .Split()
The problemis that in Regex "." is a single character. Hence, you need to escape it using "\\."
Similarly, you can use
string.split("\\.");
EDIT:
split() method of the String class uses Pattern and Matcher internally which is normally the API used for Regex in Java.
So there is no problem going with split.
In your code, 1.2.1 would be "compatible" with 3.2.0, and this looks like a serious problem, regardless if you use String.split or not. Also, you do not need to parse version numbers into integers as you only compare for equality.
StringTokenizer old_ver = new StringTokenizer(oldVersion, ".");
StringTokenizer new_ver = new StringTokenizer(newVersion, ".");
return (old_ver.nextToken().equals(new_ver.nextToken() &&
old_ver.nextToken().equals(new_ver.nextToken() );
You can surely do with String.split as well:
String [] oldV = String.split(oldVersion,"\\.");
String [] newV = String.split(newVersion,"\\.");
return oldV[0].equals(newV[0]) && oldV[1].equals(newV[1]);
The String.split() version seems slightly shorter but but for me it looks slightly more difficult to read because:
It involves additional thinking step that dot (.) is a reserved char and must be escaped.
"Next element and then following element" seems involving less thinking effort than "element at position 0 and then element at position 1".
It may pay to have some version comparator that first compares major, then intermediate and then minor parts of the version number the way that we could have the tests like "1.2.4 or later".

Splitting strings based on a delimiter

I am trying to break apart a very simple collection of strings that come in the forms of
0|0
10|15
30|55
etc etc. Essentially numbers that are seperated by pipes.
When I use java's string split function with .split("|"). I get somewhat unpredictable results. white space in the first slot, sometimes the number itself isn't where I thought it should be.
Can anybody please help and give me advice on how I can use a reg exp to keep ONLY the integers?
I was asked to give the code trying to do the actual split. So allow me to do that in hopes to clarify further my problem :)
String temp = "0|0";
String splitString = temp.split("|");
results
\n
0
|
0
I am trying to get
0
0
only. Forever grateful for any help ahead of time :)
I still suggest to use split(), it skips null tokens by default. you want to get rid of non numeric characters in the string and only keep pipes and numbers, then you can easily use split() to get what you want. or you can pass multiple delimiters to split (in form of regex) and this should work:
String[] splited = yourString.split("[\\|\\s]+");
and the regex:
import java.util.regex.*;
Pattern pattern = Pattern.compile("\\d+(?=([\\|\\s\\r\\n]))");
Matcher matcher = pattern.matcher(yourString);
while (matcher.find()) {
System.out.println(matcher.group());
}
The pipe symbol is special in a regexp (it marks alternatives), you need to escape it. Depending on the java version you are using this could well explain your unpredictable results.
class t {
public static void main(String[]_)
{
String temp = "0|0";
String[] splitString = temp.split("\\|");
for (int i=0; i<splitString.length; i++)
System.out.println("splitString["+i+"] is " + splitString[i]);
}
}
outputs
splitString[0] is 0
splitString[1] is 0
Note that one backslash is the regexp escape character, but because a backslash is also the escape character in java source you need two of them to push the backslash into the regexp.
You can do replace white space for pipes and split it.
String test = "0|0 10|15 30|55";
test = test.replace(" ", "|");
String[] result = test.split("|");
Hope this helps for you..
You can use StringTokenizer.
String test = "0|0";
StringTokenizer st = new StringTokenizer(test);
int firstNumber = Integer.parseInt(st.nextToken()); //will parse out the first number
int secondNumber = Integer.parseInt(st.nextToken()); //will parse out the second number
Of course you can always nest this inside of a while loop if you have multiple strings.
Also, you need to import java.util.* for this to work.
The pipe ('|') is a special character in regular expressions. It needs to be "escaped" with a '\' character if you want to use it as a regular character, unfortunately '\' is a special character in Java so you need to do a kind of double escape maneuver e.g.
String temp = "0|0";
String[] splitStrings = temp.split("\\|");
The Guava library has a nice class Splitter which is a much more convenient alternative to String.split(). The advantages are that you can choose to split the string on specific characters (like '|'), or on specific strings, or with regexps, and you can choose what to do with the resulting parts (trim them, throw ayway empty parts etc.).
For example you can call
Iterable<String> parts = Spliter.on('|').trimResults().omitEmptyStrings().split("0|0")
This should work for you:
([0-9]+)
Considering a scenario where in we have read a line from csv or xls file in the form of string and need to separate the columns in array of string depending on delimiters.
Below is the code snippet to achieve this problem..
{ ...
....
String line = new BufferedReader(new FileReader("your file"));
String[] splittedString = StringSplitToArray(stringLine,"\"");
...
....
}
public static String[] StringSplitToArray(String stringToSplit, String delimiter)
{
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
char[] chars = stringToSplit.toCharArray();
for (int i=0; i 0) {
tokens.addElement(token.toString());
token.setLength(0);
i++;
}
} else {
token.append(chars[i]);
}
}
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] preparedArray = new String[tokens.size()];
for (int i=0; i < preparedArray.length; i++) {
preparedArray[i] = (String)tokens.elementAt(i);
}
return preparedArray;
}
Above code snippet contains method call to StringSplitToArray where in the method converts the stringline into string array splitting the line depending on the delimiter specified or passed to the method. Delimiter can be comma separator(,) or double code(").
For more on this, follow this link : http://scrapillars.blogspot.in

Categories

Resources