extract digit from string using replace or extract method in Java - java

Example:
I/p : "its 2 3 4 5 n and ita 3 5"
O/p : 2345
So it should remove all white spaces from the digit
i tried couple of regex expression inside string.replace() but none of them worked .please help me

I think this might help
String numbers = str.replaceAll("\\D","");

You can try it like this.
String yourValue = "its 2 3 4 5 n and ita 3 5";
yourValue = yourValue .replaceAll("[^0-9]", "");
System.out.println(a);
Edit: I just realized, that your questeion is a bit confusing also. In my example the output would be "234535", which is different from what you wrote in your question. But I think there is no generalizable solution for this, that you want to cut off everything starting from the n if you really do want this.

If you want to pull all numerical values from a String then you can do something like this:
String string = "its 2 3 4 5 n and 32.45 it's a 3 5".replace(" ","");
Pattern p = Pattern.compile("-?\\d{1,3}(,?\\d{2,3})*?\\.?\\d+|-?\\d+(\\.\\d+)?");
List<String> list = new ArrayList<>();
Matcher m = p.matcher(str);
while (m.find()) {
list.add(m.group());
}
// Display the list contents.
System.out.println(list);
As you can see, this code can capture signed or unsigned Integer or floating point values from a string.
For the Regular Expression explaination, see the Tools section within this link.

Related

I want to match exactly 2 times with regex

I have something like this string.
XXXX^^^141409i1^^^XXXX.
I want to match those 3 ^ in a group and the group exactly 2 times. I wrote this but it doesn't seem to work.
(?:(\^){3}){2}
EDIT
I have to split it and extract the number in the middle. The point is that that group should consist of exactly 3 ^ and exactly 2 times. If the first group has only 1 or 2 ^ it will stop matching. That string is user input and if he inputs more than that string, for example XXXX^^^141409i1^^^XXXX^^^^XXXX then it shouldn't match the last group, only the first 2. (Sorry if I'm too ambiguous.)
EDIT2
The point of the exercise is to split the string and get the number in the middle, I wrote this line but the problem is that it matches every ^^^ and i only want to match 2 times exactly.
String[] split = s.split("(\\^){3}");
If I correctly understood what you want, I hope this will help you:
String input = "XXXX^^^141409i1^^^XXXX^^^^XXXX";
Pattern pattern = Pattern.compile(".*?\\^{3}(\\w+)\\^{3}");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
System.out.println("The number in the middle: " + matcher.group(1));
}
Output:
The number in the middle: 141409i1
Here you can see how it works: https://regexr.com/51r9e

Is regex from Java buggy or am i missing something?

With this regex :
private static String p = "^\\(([-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?))\\,([-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?))\\)$";//"^(\\-?\d+(\.\d+)?),\s*(\\-?\d+(\\.\d+)?)$";
It is impossible for me to get the values and i don't understand why...
With an input like that :
(50,180) //or even
(-50,-180)
Why my regex doesn't get me the number 180 and can get the value 50??
I mean, my Pattern object can get always the first value after parenthesis and before "," but can't get the value after ",".
What's the problem with my regex ?
My code:
private static String patternGeographicCoordinates = "^\\(([-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?))\\,([-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?))\\)$";
....
Pattern geographicCoordinates = Pattern.compile(patternGeographicCoordinates);
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
....
Matcher m1 = geographicCoordinates.matcher(line); //line is a line from a file (String)
....
if(m1.matches()){
System.out.println("IT DID WORK, LINE: "+line+", M.GROUP: "+m1.group(3));
sb.append(line);
sb.append(System.lineSeparator());
}
Why don't you just remove the parenthesis and split around the comma?
import org.apache.commons.lang3.StringUtils;
...
theString = StringUtils.strip(theString,"()"));
String[] tokens = theString.split(",");
Double number2 = Double.parse(tokens[1]);
If you want to use regex anyway, you can do it like:
Pattern p = Pattern.compile("\\(([-]?\\d+)\\s*\\,\\s*([-]?\\d+)\\)$");
String input = "(-50,-80)";
Matcher m = p.matcher(input);
if(m.find())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
See demo here
You're looking at wrong group indices. Check your regexp with this parser: https://regex101.com/
Here are the matching groups for the input (50,180):
1. [1-3] `50`
2. [1-3] `50`
5. [4-7] `180`
6. [4-7] `180`
Update
The regexp is made for more complex inputs than you supply in your example, that's why there are groups with null values. The additional groups are for decimal parts and special cases (apparently meaningful for coordinate parsing).
Look at the input (90.00,180.00). It's parsed into the following groups:
1. [1-6] `90.00`
2. [1-6] `90.00`
4. [3-6] `.00`
5. [7-13] `180.00`
6. [7-13] `180.00`
7. [10-13] `.00`
Now group 4 is matching (\.0+)? and group 7 is matching (\.\d+). You see that |90is an alternative, a special case of 90.00 degrees presumably. That's why group 3 is still empty but 4 is filled.
With input (85.21,150.34) you will get more groups filled:
1. [1-6] `85.21`
2. [1-6] `85.21`
3. [3-6] `.21`
5. [7-13] `150.34`
6. [7-13] `150.34`
8. [7-10] `150`
9. [7-10] `150`
11. [10-13] `.34`
Now group 3 is filled, but not the group 4, because it's [1-8]?\d case.
Also, since you have nested groups, same values are assigned twice: to 1 and 2 for instance.

Regex to match continuous pattern of integer then space

I'm asking the user for input through the Scanner in Java, and now I want to parse out their selections using a regular expression. In essence, I show them an enumerated list of items, and they type in the numbers for the items they want to select, separated by a space. Here is an example:
1 yorkshire terrier
2 staffordshire terrier
3 goldfish
4 basset hound
5 hippopotamus
Type the numbers that correspond to the words you wish to exclude: 3 5
The enumerated list of items can be a just a few elements or several hundred. The current regex I'm using looks like this ^|\\.\\s+)\\d+\\s+, but I know it's wrong. I don't fully understand regular expressions yet, so if you can explain what it is doing that would be helpful too!
Pattern pattern = new Pattern(^([0-9]*\s+)*[0-9]*$)
Explanation of the RegEx:
^ : beginning of input
[0-9] : only digits
'*' : any number of digits
\s : a space
'+' : at least one space
'()*' : any number of this digit space combination
$: end of input
This treats all of the following inputs as valid:
"1"
"123 22"
"123 23"
"123456 33 333 3333 "
"12321 44 452 23 "
etc.
You want integers:
\d+
followed by any number of space, then another integer:
\d+( \d+)*
Note that if you want to use a regex in a Java string you need to escape every \ as \\.
To "parse out" the integers, you don't necessarily want to match the input, but rather you want to split it on spaces (which uses regex):
String[] nums = input.trim().split("\\s+");
If you actually want int values:
List<Integer> selections = new ArrayList<>();
for (String num : input.trim().split("\\s+"))
selections.add(Integer.parseInt(num));
If you want to ensure that your string contains only numbers and spaces (with a variable number of spaces and trailing/leading spaces allowed) and extract number at the same time, you can use the \G anchor to find consecutive matches.
String source = "1 3 5 8";
List<String> result = new ArrayList<String>();
Pattern p = Pattern.compile("\\G *(\\d++) *(?=[\\d ]*$)");
Matcher m = p.matcher(source);
while (m.find()) {
result.add(m.group(1));
}
for (int i=0;i<result.size();i++) {
System.out.println(result.get(i));
}
Note: at the begining of a global search, \G matches the start of the string.

Regex issue with infix expression in Java

I'm trying to create a Java program that is able to follow the order of operations when infix expressions are entered. To simplify input I've decided it would be best to split the entire String by using a regex expression. I've been able to split everything except the 6 3 into their own String values in the splitLine array. My SSCCE attempt at this is as follows:
String line = "6 + 5 + 6 3 + 18";
String regex = "(?<=[-+*/()])|(?=[-+*/()])"; //Not spitting 6 3 correctly
String[] splitLine = line.split(regex);
for (int i=0; i<splitLine.length; i++) {
System.out.println(splitLine[i]);
}
Output:
6
+
5
+
6 3 //Error here
+
18
Expected Output:
6
+
5
+
6 //Notice
3 //these
+
18
I've tried and tried to modify my regex expression and have been unsuccessful. Could anyone tell me why my regex expression isn't splitting the 6 and the 3 into their own Strings in the splitLine array?
Edit: Just wanted to point out that I am doing this for fun, it is not for any sort of school work, etc. I just wanted to see if I could write a program to perform simple infix expressions. I do agree that there are better ways to do this and if the expression were to become more complicated I would run into some issue. But unfortunately this is how my book recommended I approach this step.
Thanks again for all of the quick comments and answers!
try this : (?<=[-+*/()])|(?=[-+*/()]|\\s{2,})
you can try adding a space in the regex, this will also split when there is 2 or more space as in this case 6 and 3 is separated by space, 6 3 will also be separated. this regex will spit the string if more than 2 space is matched. You can change the minimum number of space as \s{min,} in the regex
Following regex would match your current input.
\s(\w+|[-+*/()])
The gist is to search for a whitespace followed by a word or specific character from your list.
Output
6
+
5
+
6
3
+
18
You can use something like that maybe?
String line = "6 + 5 + 6 3 + 18";
String regex = "(?<=[-+*/() ])|(?=[-+*/() ])"; //Added space to character class
String[] splitLine = line.split(regex);
for (int i=0; i<splitLine.length; i++) {
if (splitLine[i].trim().equals("")) // Check for blank elements
continue;
System.out.println(splitLine[i].trim());
}
Or you can split your string on (?<=\\d)\\s+(?=\\d) to get 6 + 5 + 6 and 3 + 18.

get a specific number in a string

I am trying to parse a text and get a value from a text like:
Page 1 of 6
I am looking at extracting the end number using java.
so my out put in this case should be 6.
is there any java string functions I can use? (or) any other way?
You could use a regular expression for this (it's safer than using for example String.split):
public static void main(String[] args) {
String text = "Page 1 of 6";
Matcher m = Pattern.compile("Page (\\d+) of (\\d+)").matcher(text);
if (m.matches()) {
int page = Integer.parseInt(m.group(1));
int pages = Integer.parseInt(m.group(2));
System.out.printf("parsed page = %d and pages = %d.", page, pages);
}
}
Outputs:
parsed page = 1 and pages = 6.
Something like this:
String s = "Page 1 of 6";
String[] values = s.split(" ");
System.out.println(Integer.parseInt(values[values.length - 1]));
I think it is basic string manipulation. what you can do is this..
String pageNumberString = "Page 1 of 6";
int ofIndex = pageNumberString.indexOf("of");
int pageNumber = Integer.parseInt(pageNumberString.substring(ofIndex + 2));
I think this should work.
Pattern p = Pattern.compile("(\\d+)$");
Matcher m = p.match("Page 1 of 6");
System.out.println(Integer.parseInt(m.group(1)));
I'd use a regular expression, as long as the format of your numbers is going to stay similar.
This one for example, will match any string with 2 numbers (seperated by any non-digit character), and capture the 2 numbers.
(\d+)[^\d]+(\d+)
Note: this will match some weird things like "Page1of2". It also won't match negative numbers. Not that you expect to ever get a negative page number.

Categories

Resources