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
}
Related
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
In Java, I have to validate a string which contains "~" and '=' at the end using RegEx.
For example:
LOCKER=2004-02-23-23.28.22.377655~UCC=0103207031~URY=31/12/9999~URF=23/02/2004~URT=SEREST ISSY LES MO ~URFC=XX~URFNUMCB=XXXXXXXXXXX~CEB=XXXXX~CEBC=XXXXX~URFN=0001
this String format is KEY1=VALUE~KEY2=VALUE~KEYN=VALUE uppercase
'~' is as separator
Currently, i am using some regular expresion but all of them false
can anyone help me please ? thank you for advanced
The following regex should do:
^(?!~)(?:(?:^|~)[^=~]+=[^=~]*)+$
Explanation
^ Match beginning-of-input, i.e. matching must start at beginning
(?!~) Input cannot start with `~`
(?: Repeat 1 or more times:
(?:^|~) Match beginning of input or match '~', i.e. match nothing on
first repetition, and match `~` on each subsequent repetition
[^=~]+ Match KEY
= Match '='
[^=~]* Match VALUE (may be blank)
)+
$ Match end-of-input, i.e. matching must cover all input
Change the characters classes for KEY and VALUE as needed if they have further restrictions, e.g. use [A-Z][A-Z0-9]* instead of [^=~]+ if the KEY has to be an uppercase-only identifier.
If using Java's matches() with the regex, the first ^ and the ending $ is redundant. The second ^ is still required.
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 5 years ago.
Improve this question
I am confuse in the logic behind the code (?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
it is separating numbers and alphabets like input String abc12dc23 then it is spliting it as output abc 12 dc 23.
I just want the explanation how the above code is working?
This regex:
(?<=\D)(?=\d)|(?<=\d)(?=\D)
matches 2 kinds of patterns, as suggested by the | character:
This pattern:
(?<=\D)(?=\d)
and this pattern:
(?<=\d)(?=\D)
The former looks for a position in the string where there is a non-digit (\D) character before that position and a digit (\d) after it. The latter looks for a position where the reverse happens, a digit before and a non-digit after.
To say this in a more abstract way, the regex is looking for digit-non-digit boundaries.
The split method looks for all occurrences of the pattern and splits the string when it finds one.
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
I have a few, theoretical ideas, but I don't know the language well. We basically need to make a rudimentary lexical analyzer. I have most of the bits, but here's the context. The straight-forward question is at the end.
I need to read in a line from the console, then see if parts match up with a symbol table, which includes the keywords: "print [variable]", "load [variable]", "mem [variable]", "sqrt" and "stop", as well as mathematical symbols.
It also needs to recognise the variables on their own (such as "c = a + b" as well.)
So...it's not that hard, in theory. You'd check the first character of the string matched up with keywords or variables. If they do, keep looping through that keyword or variable to check if it's the same string, up until you hit a space.
To summarize: How do I check the characters of a read in string to compare to stuff in Java?
I recommend to use Regex for text pattern matching. You receive the text via console as argument, you do so by using the args array of the main-method. Here's a small example:
public final class Parser {
public static void main(final String[] args) {
if (args.length < 1) {
// Error, no input given
}
String input = args[0];
Pattern pattern = Pattern.compile("YOUR REGEX HERE");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
// Input matches the Regex pattern
// Access to capturing groups using matcher.group(int)
// Example: System.out.println(matcher.group(1));
}
}
}
For Regex you can find various explanations on the web and on SO. You can try out your patterns at regex101.
Here's an example pattern which matches "name = name + name":
(.+) = (.+) \+ (.+)
The () creates capturing groups. Using matcher.group(x) for x from 1 to 3 you can access the matched values inside the brackets, i.e. the variables.
Here's the same example online with test input: regex101.com/r/mJ9jI5/1
Fairly easy. However you may need to make the pattern more robust. It may not accept whitespace characters or special characters (for example a +) inside a variable name and so on.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to write a regex that will find everything except for '.' - that is, a string which only contains '.' should return false and everything else true.
String regex = "(?!(^\\.$)).*";
String test = ".";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(test);
System.out.println(matcher.find());
^(?![.]$).*$
This should do it for you.You need to use anchors to make sure you dont make any partial matches.
I think this will do the job [^.].*|[.].+, i tried 15 different inputs on http://www.regexplanet.com/advanced/java/index.html, and it only says false when trying to find '.'
Explanation:
[^.].* - match everything that starts with no-dot character, and after that has 0 or more of any other characters,
[.].+ - match everything that starts with dot, and is followed by at least one or more of any other characters.
[^.].*|[.].+ - both parts merged with operator 'or'
Do you really need a regex for this, just use string comparison:
if (!".".equals(test)) {
// not equal to a dot
}
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