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
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 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 have a task to check an ID that must start with "ASSOC" in uppercase followed by 3 digits. I am a newbie in Java and still learning regex so any help would be welcome!
The following regular expression matches any string that matches the pattern you want: ASSOC[0-9]{3}.
If you also want to extract the 3-digit ID as a so-called regex-group from the match, the expression must be as follows: ASSOC([0-9]{3}).
The [0-9] says here, that we expect a number-character (digit) of 0-9. The curly brackets also express that exactly 3 digits, therefore a 3-digit number is expected.
On Regex101 you also have the possibility to validate different inputs with this regex. Furthermore the regular expression is explained in detail.
Example 1: https://regex101.com/r/zEN9Gt/1
Example 2: https://regex101.com/r/zEN9Gt/2
In Java you could test this as follows:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "ASSOC([0-9]{3})";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher1 = pattern.matcher("ASSOC123");
final Matcher matcher2 = pattern.matcher("Assoc123");
if(matcher1.find()) {
System.out.println(matcher1.group(1));
// Since ASSOC123 is a valid input for the regular expression,
// matcher1.find() is true and we can output the 3 digit number (attention: group 1!).
// Group 0 would be the entire expression found, hence "ASSOC123".
}
if(matcher2.find()) {
// Since "Assoc123" does not match the regular expression, matcher2.find() is false
}
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);
}
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 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