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 2 years ago.
Improve this question
I would like to ask you about regex expression - I need to get all numbers that occur to a certain character. For example:
"$z4~min.~00~s" -> 4
"$z12~min.~00~s" -> 12
I simply need first number in the string, I don't need numbers after dot in the string.
I am using Java for this project.
Do you have any suggestions? Thanks a lot.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^\\D*(\\d+)");
java.util.regex.Matcher matcher = pattern.matcher("$z12~min.~00~s");
if (matcher.find()) {
String firstNumber = matcher.group(1);
System.out.println(firstNumber);
}
Related
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
I'm trying to find the regex to get the last letter in a string:
String str = "A76B62Z**F**63";
Finding last letter via regex should return 'F'.
You can greedily match any sequence of characters before a letter:
String s = "A76B62ZF63";
Matcher m = Pattern.compile(".*([A-Za-z])").matcher(s);
if(m.find()) System.out.println(m.group(1));
With Java 9+:
String s = "A76B62ZF63";
Pattern.compile(".*([A-Za-z])").matcher(s).results()
.findFirst().ifPresent(r -> System.out.println(r.group(1)));
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 4 years ago.
Improve this question
I have the following string:
___abcd-metadata.json
I am trying to do a regex to get ___ and everything after -. So far I have this Regex:
(\-.*?\.json)
To find the last part and (___) to find the first part, but I have not been able to figure how to combine both regexes to make this happen.
My desired result would be ___ -metadata.json = true
"(\-)(.*)" is the pattern, which will give you 2 matches if the string contains them.
Take your string and use String.split('-') to break it into parts.
EDIT
var parts = ('_abcd-metadata.json').split('-'),
start = parts[0].substr(0,3),
rest = parts[1];
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've got a string like this:
1454974419:1234;1454974448:3255,2255,66789
I would like to extract/group these values by using regular expression in java.
1234
3255225566789
You can use this lookbehind and negation based regex:
(?<=[:,])[^;,]+
RegEx Demo
Breakup:
(?<=[:,]) # lookbehind to assert if previous char is : or ,
[^;,]+ # match 1 or more of anything that is not a ; or ,
Try this
String yourString= "1454974419:1234;1454974448:3255,2255,66789";
Pattern myPattern = Pattern.compile("[^a-zA-Z0-9]");
Matcher myMatcher = myPattern.matcher(yourString);
while(myMatcher.find())
{
String temp= myMatcher.group();
yourString=yourString.replaceAll("\\"+temp, "");
}
System.out.println(yourString);
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 need to take out all the characters of a string that are not numbers.
You could use a Character Class Intersection like [\p{Punct}\p{Lower}\p{Upper}&&[^.]]
But why not just use
[^\d.]+
As Java String "[^\\d.]+"
This would match one or more characters, that are not \d a digit or the . period.
I'd suggest using \\d+ then (it's consecutive digits), and a capture group. Something like
String str = "";
str = str.replaceAll("(\\d+\\.\\d+)", "$1");
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 9 years ago.
Improve this question
I am having one string containing "This is a time to get involve into $FO, $RTP, $DFG and $RG"
Use following regular expression:
"\\$\\w+"
$ should be escaped.
\w match digits, alphabet, _.
If you need only match alphabets, use [a-zA-Z] instead.
This will work too
String str="This is a time to get involve into $FO, $RTP, $DFG and $RG" ;
String[] arr=str.split(" ");
for (String i:arr){
if(i.indexOf("$")==0){
System.out.println(i.replaceAll("\\,",""));
}
}