Turn zeroes in a string to spaces - java

Suppose I have a string:
String s = "34205478200044520042";
I want to change this string so that a 0 becomes a space, but consecutive 0s are treated as only 1 space still.
Meaning the new string would look like "342 54782 4452 42"
How do you treat any number of 0s to equal one space?

The regular expression 0+ matches any number of zeroes, because + in a regular expression means "one or more of the preceding subexpression". So you can write
String newString = myString.replaceAll("0+", " ");

You can use the replaceAll method to replace a regular expression of a sequence of 0 characters with a space:
String result = s.replaceAll("0+", " ");

You can use regular expressions to achieve this:
System.out.println(s.replaceAll("[0]+", " "));
// + indicates that the character set (in this case "0") must match at least once.
// Displays 342 54782 4452 42

Related

How to remove space after the decimal point without removing other spaces?

I have one String that has multiple values in single string. I just want to remove the space after decimal point only without removing other spaces from string.
String testString = "EB:3668. 19KWh DB:22. 29KWh";
testString = testString.trim();
String beforeDecimal = testString.substring(0, testString.indexOf("."));
String afterDecimal = testString.substring(testString.indexOf("."));
afterDecimal = afterDecimal.replaceAll("\\s+", "");
testString = beforeDecimal + afterDecimal;
textView.setText(testString);
Here as in my string there is two values in single string
EB:3668. 19KWh and DB:22. 29KWh.
I just want to remove space after decimal point and make String like this:
EB:3668.19KWh DB:22.29KWh
You can use 2 capture groups and match the space in between. In the replacement use the 2 groups without the space.
(\d+\.)\h+(\d+)
Regex demo
String testString="EB:3668. 19KWh DB:22. 29KWh";
String afterDecimal = testString.replaceAll("(\\d+\\.)\\h+(\\d+)","$1$2");
System.out.println(afterDecimal);
Output
EB:3668.19KWh DB:22.29KWh
Or a bit more specific pattern could be including the KWh:
\b(\d+\.)\h+(\d+KWh)
Regex demo
Just use string.replaceAll("\\. ", ".");
Thanks to Henry for pointing out I had to escape the .
I'm not in front of an editor right now, but wouldn't you be able to do this with the replaceAll method in a single line, without breaking it up?
var text = testString.replaceAll(". ", ".");
You can remove unnecessary spaces between the decimal point and the fractional part as follows. This code also removes other extra spaces:
String testString = " EB:3668. 19KWh DB:22. 29KWh ";
String test2 = testString
// remove leading and trailing spaces
.trim()
// replace non-empty sequences of space
// characters with a single space
.replaceAll("\\s+", " ")
// remove spaces between the decimal
// point and the fractional part
// regex groups:
// (\\d\\.) - $1 - digit and point
// ( ) - $2 - space
// (\\d) - $3 - digit
.replaceAll("(\\d\\.)( )(\\d)", "$1$3");
System.out.println(test2); //EB:3668.19KWh DB:22.29KWh
See also: How do I remove all whitespaces from a string?

How do i write regex pattern for a String to identify numbers that precedes space or hypen?

I have a free flowing string that has some random text like below:
"Some random text 080 2668215901"
"Some ramdom text 040-1234567890"
"Some random text 0216789101112"
I need to capture the the 3 digit numbers and the following 10 digit numbers:
with space condition
with hypen condition
without any space/hypen
I am using Java.
This is what I tried to get the numbers from the free flowing text:
"\\w+([0-9]+)\\w+([0-9]+)"
I can do a string length check to see if there are any 3 digit numbers that precedes a Hypen or a space, which is then followed by a 10 digit number.But i really would like to explore if regex can give me a better solution.
Also,if there are more occurances within the String,i'd need to capture them all. I would also need to capture any 10 digit String as well,that need not precede a hypen and a space
It is usually (\d{3})[ -]?(\d{10})
With boundary conditions maybe (?<!\d)(\d{3})[ -]?(\d{10})(?!\d)
Assuming you'll run this regex on individual lines, and ignoring some of the... more expressive regex implementations, this is perhaps the simplest way:
/([0-9]{3})[ -]?([0-9]{10})/
If your text might end in numbers, you'll need to anchor the result to the end of the line like this:
/([0-9]{3})[ -]?([0-9]{10})$/
If you are guaranteed literal double quote characters around your inputs, you could instead use:
/([0-9]{3})[ -]?([0-9]{10})"$/
And if you needed to match the entire line for some input error testing, you could use:
/^"(.+)([0-9]{3})[ -]?([0-9]{10})"$/
Here is a longer demo. From your responses above you're also looking for matches with trailing chars after the match.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Class {
private static final Pattern p = Pattern.compile("" +
"((?<threeDigits>\\d{3})[- ]?)?" +
"(?<tenDigits>\\d{10})");
public static void main(String... args) {
final String input =
"Here is some text to match: Some random text 080 2668215901. " +
"We're now matching stray sets of ten digit as well: 1234567890. " +
"Notice how you get the first ten and the second ten, with the preceding three:1234123412-040-1234567890" +
"A stranger case:111222333444555666777888. Where should matches here begin and end?";
printAllMatches(p.matcher(input));
}
private static void printAllMatches(final Matcher m) {
while (m.find()) {
System.out.println("three digits: " + m.group("threeDigits"));
System.out.println("ten digits: " + m.group("tenDigits"));
}
}
}
switched to findall battleplan.

How can I strip all non digits in a string except the first character?

I have a string that I want to make sure that the format is always a + followed by digits.
The following would work:
String parsed = inputString.replaceAll("[^0-9]+", "");
if(inputString.charAt(0) == '+') {
result = "+" + parsed;
}
else {
result = parsed;
}
But is there a way to have a regex in the replaceAll that would keep the + (if exists) in the beginning of the string and replace all non digits in the first line?
The following statement with the given regex would do the job:
String result = inputString.replaceAll("(^\\+)|[^0-9]", "$1");
(^\\+) find either a plus sign at the beginning of string and put it to a group ($1),
| or
[^0-9] find a character which is not a number
$1 and replace it with nothing or the plus sign at the start of group ($1)
You can use this expression:
String r = s.replaceAll("((?<!^)[^0-9]|^[^0-9+])", "");
The idea is to replace any non-digit when it is not the initial character of the string (that's the (?<!^)[^0-9] part with a lookbehind) or any character that is not a digit or plus that is the initial character of the string (the ^[^0-9+] part).
Demo.
What about just
(?!^)\D+
Java string:
"(?!^)\\D+"
Demo at regex101.com
\D matches a character that is not a digit [^0-9]
(?!^) using a negative lookahead to check, if it is not the initial character
Yes you can use this kind of replacement:
String parsed = inputString.replaceAll("^[^0-9+]*(\\+)|[^0-9]+", "$1");
if present and before the first digit in the string, the + character is captured in group 1. For example: dfd+sdfd12+sdf12 returns +1212 (the second + is removed since its position is after the first digit).
try this
1- This will allow negative and positive number and will match app special char except - and + at first position.
(?!^[-+])[^0-9.]
2- If you only want to allow + at first position
(?!^[+])[^0-9.]

Regular Expression - inserting space after comma only if succeeded by a letter or number

In Java I want to insert a space after a String but only if the character after the comma is succeeded by a digit or letter. I am hoping to use the replaceAll method which uses regular expressions as a parameter. So far I have the following:
String s1="428.0,chf";
s1 = s1.replaceAll(",(\\d|\\w)",", ");
This code does successfully distinguish between the String above and one where there is already a space after the comma. My problem is that I can't figure out how to write the expression so that the space is inserted. The code above will replace the c in the String shown above with a space. This is not what I want.
s1 should look like this after executing the replaceAll: "428.0 chf"
s1.replaceAll(",(?=[\da-zA-Z])"," ");
(?=[\da-zA-Z]) is a positive lookahead which would look for a digit or a word after ,.This lookahead would not be replaced since it is never included in the result.It's just a check
NOTE
\w includes digit,alphabets and a _.So no need of \d.
A better way to represent it would be [\da-zA-Z] instead of \w since \w also includes _ which you do not need 2 match
Try this, and note that $1 refers to your matched grouping:
s1.replaceAll(",(\\d|\\w)"," $1");
Note that String.replaceAll() works in the same way as a Matcher.replaceAll(). From the doc:
The replacement string may contain references to captured subsequences
String s1="428.0,chf";
s1 = s1.replaceAll(",([^_]\\w)"," $1"); //Match alphanumeric except '_' after ','
System.out.println(s1);
Output: -
428.0 chf
Since \w matches digits, words, and an underscore, So, [^_] negates the underscore from \w..
$1 represents the captured group.. You captured c after , here, so replace c with _$1 -> _c.. "_" represent a space..
Try this....
public class Tes {
public static void main(String[] args){
String s1="428.0,chf";
String[] sArr = s1.split(",");
String finalStr = new String();
for(String s : sArr){
finalStr = finalStr +" "+ s;
}
System.out.println(finalStr);
}
}

Check if a String only contains digits and the digits are not the same using REGEX?

111111111 - Invalid
A121278237 - Invalid
7777777777 - Invalid
121263263 - Valid
111111112 - Valid
^([0-9])(?!\1+$)[0-9]+$
should work. It needs a string of at least two digits to match successfully.
Explanation:
Match a digit and capture it into backreference #1: ([0-9])
Assert that it's impossible to match a string of any length (>1) of the same digit that was just matched, followed by the end of the string: (?!\1+$)
Then match any string of digits until the end of the string: [0-9]+$
EDIT: Of course, in Java you need to escape the backslash inside a string ("\\").
take a [0-9] regex and throw away strings that not only contain digits.
take the first character, and use it as a regex [C]+ to see if the string contains any other digits.
Building on Tim's answer, you eliminate the requirement of "at least two digits" by adding an or clause.
^([0-9])(?!\1+$)[0-9]+$|^[0-9]$
For example:
String regex = "^([0-9])(?!\\1+$)[0-9]+$|^[0-9]$";
boolean a = "12".matches(regex);
System.out.println("a: " + a);
boolean b = "11".matches(regex);
System.out.println("b: " + b);
boolean c = "1".matches(regex);
System.out.println("c: " + c);
returns
a: true
b: false
c: true

Categories

Resources