Regex to get the last value [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
From the following value
[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]
I want to get the last number 2196879032
Some times value will be -1
[AAMP-PLAYER]aamp pos: [14..187..202..-1]
Instead of split method how can i extract last digit using regex method

You could match the last (possibly negative) number at the end of the string, just before the closing ]:
(\-?\d+)\]$

A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
(\-?\d+)(?!.*\d)

We could try using a String#replaceAll option here:
String input = "[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]";
String num = input.replaceAll("^.*\\.\\.(-?\\d+).*$", "$1");
System.out.println(num);
This prints:
2196879032

Related

Find all the occurences of an array that contain a substring in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Input:
[the, and, land, wander, dreams]
Substring: "and"
Output: 3
I need to find all the occurrences of an array that contain a certain substring but all I found was the word itself. For example, I want it to count the "and" in the word "land" and "wander" as well. I don't know how to do that. Please help!
EDIT: Updated the code.
What about:
int cnt=0;
String[] input = {"the", "and", "land", "wander", "dreams"};
for (String str : input){
if (str.contains("and")){
cnt++;
}
}
System.out.println(cnt);
This code should work for you. But keep in mind following - Exbow is right, next time your question might be considered as useless and will be closed.

Deleting lines from String containing a certain character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
any help with this would be greatly appreciated.
code in Java SE 8.
A string is inputted to represent a table.
if a row in the table contains the word NA it must be removed. ex..
if given String = "id,name,age,score\n1,Tom,NA,20\n17,Susan,25,15"
the table would be displayed as follows;
id name age score
1 Tom NA 20
5 Susan 25 15
As the second row contains 'NA' it needs to be removed. Ps Case matters, so if it says 'na' it doesn't need removed.
I have tried the following:
input = input.replaceAll("NULL,", "");
String lines[] = input.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
The problem is I need to remove the entire row where there is an occurrence of the word 'NA'
any ideas?
I recommend splitting the String with String.split('\n'); That will return an Array you can work with much easier. With that you can just filter the array for Strings that include NA.

Remove Known Part of String Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
A little help please. I want to strip-off a portion of my string which is made up of a number in Java.
eg. Have the following Strings - 60067 ; 600567 and 600876 and '600676600'
How do I remove 600 from each of the strings in Java so I remain with
67 ; 567 and 876 and '676600'
As Pavneet said in his comment, this works well:
"60067".substring(3);
Also, if you want to make sure it is the string "600" that you are removing:
"60067".replaceFirst("600", "");
Java Strings have no remove method, so it's common to replace the String with a blank one, "".
try this:
String s = "60067 ; 600567 and 600876";
s = s.replaceAll("600", "");
System.out.println(s);
I got
67 ; 567 and 876

How to alter Java strings contain only certain alphabetical characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
how can I alter text string fields in java to only contain certain alphabetical characters (f,-)
in this format: 2f5-4, 2f6, 8f9
Only numbers f numbers and
numbers f number - numbers
You could use a regular expression to check, if the String in your text field is valid:
\d+f\d+(?:-\d+)?
Java code sample
Pattern pattern = Pattern.compile("\\d+f\\d+(?:-\\d+)?");
for (String s : new String[] {
"2f6", "2f9", "6f10", "5f9-2", "3f4-9"
}) {
System.out.println("String: \""+s+"\" match: "+pattern.matcher(s).matches());
}
Regex expression =>
(([0-9]+)([f])([0-9]*)|(([-])([0-9]*)))\w+

Changing the Lengths of Words in a String - Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Categories

Resources