Find last letter in String using Regex [closed] - java

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)));

Related

Regex - Find all the digits that occur to a certain character [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 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);
}

RegEx to extract/group the all values which comes after colon from below string 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 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);

Is there a way to use \p{Punct}\p{Lower}\p{Upper} and in a regex(java), but without the "." 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 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");

java regexp for reading number after certain symbol [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 have the following string
String names= D:45454546544654 A:45454545454 C:454545474
I need Output to be
String data[]=[45454546544654,45454545454,454545474]
First replace all the uppercase letter plus the following colon : with an empty string and then split the resultant string according to the spaces.
String names = "D:45454546544654 A:45454545454 C:454545474";
String parts[] = names.replaceAll("[A-Z]:", "").split("\\s+");
System.out.println(Arrays.toString(parts));
Output:
[45454546544654, 45454545454, 454545474]
Rather than split you can just match:
(?<=:)\d+(?!\d)
Using this Pattern:
Pattern p = Pattern.compile("(?<=:)\\d+(?!\\d)");
Then you can use Matcher.find API to get all the matches.
RegEx Demo

Pattern matching for validating the input [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 8 years ago.
Improve this question
I have to validate the input as
Asterisks are permitted in positions 2-5.
Position One should be alphabetic (except for the ~)
-No characters should accept numbers.
Other than the exceptions mentioned above, no special characters are allowed
I am trying to build as this.
final Pattern pattern =
Pattern.compile("^[a-zA-Z~][a-zA-Z*]*$", Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(this.mainStaOrgBO.getStaOrgCode());
final boolean specialCharCheck = matcher.find();
if (specialCharCheck) {
}
How about:
^[a-zA-Z~][a-zA-Z*]{1,4}[a-zA-Z]*$
Explanation:
^ : start of string
[a-zA-Z~] : First char can be letter or ~
[a-zA-Z*]{1,4} : char 2 to 5 can be letter or *
[a-zA-Z]* : rest of string only letter
$ : end of string.
This should work
[a-zA-Z~]\*[a-zA-Z~]{2}\*[a-zA-Z~]*
If the * are optional
[a-zA-Z~][a-zA-Z~\*][a-zA-Z~]{2}[a-zA-Z~\*][a-zA-Z~]*
Test is here Online Java Regex Test

Categories