How to remove numbers from string which starts and ends with numbers? - java

I have two strings
1. J2EE
2. java1.6
I want to remove numbers from only the start and ends; not from between.
Can anyone provide a regular expression for this, or any other solution?

simple using replaceAll() using ^\\d+|\\d+$ regex that looks for digits in the beginning and ending of the line.
System.out.println("1adfds23dfdsf121".replaceAll("^\\d+|\\d+$", ""));
output:
adfds23dfdsf
EDIT
Regex explanation:
^ Start of line
\d+ Any digit (one or more times)
| OR
\d+ Any digit (one or more times)
$ End of line

Use this:
String replaced = yourString.replaceAll("^\\d+\\. |(?:\\d+\\.)?\\d+$", "");
Output:
J2EE
java
Explanation
^\d+\. matches the leading digits, period and space
'|' OR
(?:\d+\.)?\d+$ matches optional digits-and-dot, digits, and end of string
Replace with the empty string

Use this:
string.replaceAll("^(?:\\d|\\d\\.\\d)*|(?:\\d|\\d\\.\\d)*$", "");
This looks for digits at the end and/or start of the string, and replaces them with nothing. It will also remove decimal dots (.)
^ Start of string
(?:\\d|\\d+\\.\\d+)* a digit or a number with a decimal (1234.4321)
* zero or more times
| or
$ end of the string

Related

Regex to match lines having specific count of Decimal numbers

I want to match the lines having specific count of decimal numbers separated by spaces. Say lines having only 3 decimal numbers. Consider the below example:
Abc 1.56 1.67 5.67
xyz 4.51 12.43 32.50
03/31/2019 $1234 $(1234) $60,501 5.81 7.81
abcdf $123,345 $123 $123,149
For this given input I want to fetch only the first two lines as they have only contains 3 decimal numbers separated by space. I have tried (.*[\s0-9.]+)$ && ([\s0-9.]+)$ but with these I end up getting many other unwanted lines as well.Could some one please advise if this is something we can do using regular expression.
Using [\s0-9.]+ is a broad match for a decimal number, as it could also match only newlines, spaces or dots.
If there can also digits occur before the decimals, you could use:
^.*?\d+\.\d+(?:\h+\d+\.\d+){2}$
Explanation
^ Start of string
.*? Match any char except an newline non greedy
\d+\.\d+ Match a decial number
(?:\h+\d+\.\d+){2} Repeat 2 times matching 1+ horizontal whitespace chars and a decimal number
$ End of string
In Java
String regex = "^.*?\\d+\\.\\d+(?:\\h+\\d+\\.\\d+){2}$";
Regex demo
If the digit before the decimal is optional, you could change it to \d*\.\d+
To match lines starting with some text, then three decimal numbers separated by spaces, use this regex:
(?m)^(\p{L}+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h*$
Remember to double the \ when inserting into a Java string literal.
See regex101 for demo.

Regex command to remove numbers after decimal place and replace other special character such as £?

Regex command to remove numbers after decimal place and replace other special character such as £?
Example Before the change has been implemented: "£233.555"
After change has been implemented (£ removed, numbers after decimal place removed): "233"
I have the following code but dosnt seem to do the job, please note im using the regex replaceAll within java as listed:
Long.parseLong(list.get(i).getText().replaceAll("[£ [.0]+$]", ""
You can try this regex :
£(\d+)\.\d+
and replace with $1
Live demo here
Sample java code
String val = "£233.555";
System.out.println(val.replaceAll("£(\\d+)\\.\\d+", "$1"));
// OUTPUT : 233
Explanation
£(\d+): Match £ followed by 0+ digits, capture only the digits.
\.\d+ Match a dot followed by 0+ digits, don't capture anything.

Regular Expression that matches number with max 2 decimal places

I'm writing a simple code in java/android.
I want to create regex that matches:
0
123
123,1
123,44
and slice everything after second digit after comma.
My first idea is to do something like that:
^\d+(?(?=\,{1}$)|\,\d{1,2})
^ - from begin
\d+ match all digits
?=\,{1}$ and if you get comma at the end
do nothin
else grab two more digits after comma
but it doesn't match numbers without comma; and I don't understand what is wrong with the regex.
You may use
^(\d+(?:,\d{1,2})?).*
and replace with $1. See the regex demo.
Details:
^ - start of string
-(\d+(?:,\d{1,2})?) - Capturing group 1 matching:
\d+ - one or more digits
(?:,\d{1,2})? - an optional sequence of:
, - a comma
\d{1,2} - 1 or 2 digits
.* - the rest of the line that is matched and not captured, and thus will be removed.
basic regex : [0-9]+[, ]*[0-9]+
In case you want to specify min max length use:
[0-9]{1,3}[, ]*[0-9]{0,2}
Here:
,{1}
says: exactly ONE ","
Try:
,{0,1}
for example.

regex to match a recurring pattern

I am trying to write a regex for java that will match the following string:
number,number,number (it could be this simple or it could have a variable number of numbers, but each number has to have a comma after it there will not be any white space though)
here was my attempt:
[[0-9],[0-9]]+
but it seems to match anything with a number in it
You could try something along the lines of ([0-9]+,)*[0-9]+
This will match:
Only one number, e.g.: 7
Two numbers, e.g.: 7,52
Three numbers, e.g.: 7,52,999
etc.
This will not match:
Things with spaces, e.g.: 7, 52
A list ending with a comma, e.g.: 7, 52,
Many other things out of the scope of this problem.
I think this would work
\d+,(\d+,)+
Note that as you want, that will only capture number followed by a comma
I guess you are starting with a String. Why don't you just use String.split(",") ?
^ means the start of a string and $ means the end. If you don't use those, you could match something in the middle (b matched "abc").
The + works on the element before it. b is an element, [0-9] is an element, and so are groups (things wrapped in parenthesis).
So, the regex you want matches:
The start of the string ^
a number [0-9]
any amount of comas flowed by numbers (,[0-9])+
the end of the string $
or, ^[0-9](,[0-9])+$
Try regex as [\d,]* string representation as [\\d,]* e.g. below:
Pattern p4 = Pattern.compile("[\\d,]*");
Matcher m4 = p4.matcher("12,1212,1212ad,v");
System.out.println(m4.find()); //prints true
System.out.println(m4.group());//prints 12,1212,1212
If you want to match minimum one comma (,) and two numbers e.g. 12,1212 then you may want to use regex as (\d+,)+\d+ with string representation as \\d+,)+\\d+. This regex matches a a region with a number minimum one digit followed by one comma(,) followed by minimum one digit number.

What does this syntax for regular expressions mean?

Can someone tell me what the regular expression in the following Java code snippet means:
String someString = …;
someString.matches("^\\d{5}-\\d{4}$");
This will match 5 decimal numbers at the beginning of the string, followed by a dash, followed by 4 decimal numbers at the end.
^ = Beginning of string
\d{n} = Match n decimal numbers
$ = End of string
From http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
five digits, a dash, and then four more digits...nothing else
^ means beginning of line.
\d{5} means five digits.
- literally means "-"
\d{4} means four digits.
$ means end of line.
So it's looking for a sequence of five digits followed by a sequence of four digits, seperated by a dash and that is the only thing on the line.
Example:
12345-6789

Categories