I am developing a Java method, which checks, if the user input is valid. For that I wrote four regular expressions to check that. The first three work fine. The last one, the "most complex" regular expression, does not accept values, which should be accepted.
I want to make sure, that the user entered one of three different input settings.
One to six "L" followed by a number from 1-16
One to six "R" followed by a number from 1-16
One B followed by a number from 1-16
My problem is to define, that only numbers from 1-16 will be accepted. My regular expression is accepting 1-9, but not any number above 9.
Let's take the "B"-case for example, than this is my regular expression:
String regexB = "B[([1-9]{1})((1[0-6]){1})]";
What I tried to do with my expression:
One "B", followed by one single number from 1-9 OR by a "1" and a second single number from 0-6.
I know, that this is possibly not a hard question, but maybe one of you guys is able to save me from losing some time by trying to solve this.
Thanking you in anticipation.
String regexB = "B([1][0-6]|[1-9])";
B
a digit 1 and 0-6 OR
a digit between 1 and 9
I think your regexp is invalid.
Try the following (for B example)
B((1{1}[0-6]{1})|([1-9]{1}))
String regexB = "B([1-9]|([1][0-6]))";
edit: oh ... the answer is already there ...
Here is a tool that will help you: http://utilitymill.com/utility/Regex_For_Range.
In your case: [1-9]|1[0-6]
Related
I am trying to find a regex for the following user generated possibilities:
÷2%3%x#4%2$#
OR
÷2%x#4%2$#
OR
÷2%x#4$#
OR
÷2%x#
To understand the expression, it is a fraction whose numerator lies between
the ÷ and the first %, and the denominator lies from first % to the #.
But, the denominator has an exponent, which lies from the # to $.
The user can input whatever number he/she desires, but the structure stays the same. Notice that the number can also be a decimal.
The structure is as follows: ÷(a number, if its two or more digits a % will be in between the digits)x(a group that consists of a number(s), also the symbols # , $ and a %(s) which can also alternate between the digits)#
Remember, the number can be a decimal number.
I am trying to use the following regex with no success:
"[÷]-?\\d+(\\.\\d*)?[%](-?\\d+(\\.\\d*)?){0,1}[x]([#]-?\\d+(\\.\\d*)?[$]){0,1}[#]"
I think that the group (-?\d+(\.\d*)?){0,1} is complicating things up.
Also, I have not accounted for the % within this group which could occur.
any suggestions, thank you
Edit: Deleted the old post content.
According to your new testcases I improved your regex to match all cases and simplified the regex:
÷[0-9%]+?x(#[0-9%]+?\$)?# OR ÷[\d%]+?x(#[\d%]+?\$)?#
Note:
The [] mark groups of allowed characters so it has no use to have the parenthesis.
Also [÷][0-9]+[0-9[%]]+? is just the same as ÷[0-9]+[0-9%]+? the first part in your example matches any number 0-9 n-times and then you check for either (0-9 or %) for n-times (non greedy fetching). So instead you can just use the second check for the whole thing.
By wrapping the exponent in a regex-class: () we can make the whole exponent optional with ? ==> this will make your 4th test-case work.
You could also substitute 0-9 with \d (any digit) if you want.
I found a regex that works, I tested from the bottom up:
Here it is:
[÷][0-9[%][\\.]]+?[x][0-9[%][\\.][#][$]]*?[#]
This regex works for all types of cases. Even those that include decimal numbers, or not exponents.
the group [0-9[%][\.][#][$]]*? allows the regex to search for exponent, which can occur zero(that's why the * is there) or more times and the ? makes it optional. Similarly, I followed the same idea for the coefficient of x(read the post if you don't know where the coefficient lies) and the numerator. Thank you for everyone that put effort in brainstorming this problem. I have chosen to use my answer for my programming.
Here's my RegExp checker which currently doesn't work:
String pattern = "(?=.*[0-9]{3})(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]{1})(?=\\S+$).{5,15}";
Here are the parameters I cannot meet yet:
5-15 characters {5,15}
exactly 3 digits (?=.*[0-9]{3})
Neither the character limit nor the digit check are working, and I can't find any examples for some reason. Where I am I going wrong? Clearly it's a placement issue, as I'm a total novice. Any help would be appreciated. The others (at least one upper/lowercase/special) I can meet, but these two simple pieces I'm still struggling with.
For three digits checking add this anywhere in your regex as you are using positive lookahead.
(?=^([^0-9]*[0-9]){3}[^0-9]*$)
For 5-15 digit check add this one:
(?=^.{5,15}$)
You can use the regex on the site https://regex101.com/ and it will give you the explanation on the right hand side.
[0-9]{3} is 3 consecutive integers. To allow three integers somewhere in the string you need to check for each integer part.
(?=^[^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*$)
.{5,15} is 5 to 15 characters but that is anywhere in the string, to have it affect the whole string that needs to be anchored. So your full expression should be:
^(?=^[^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*$)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]{1})(?=\\S+$).{5,15}$
Demo: https://regex101.com/r/UVK7ev/1
I want to know if there is a way to check if a string contains a certain pattern for a regex.
For example:
string.matches("something[0-9]x") would check if the string contains a substring of "something" with any single digit integer following it followed by "x". But lets say if I want to check the same thing, but there is no limit for that int, ie it could be 1000000. Is there like a wildcard for an int that I can use?
Just use modifier + after your character class which match the preceding token one or more time :
string.matches("something[0-9]+x")
Regular expressions work on characters; they have no semantic understanding of those characters. So it doesn't make sense to talk about "integers" here; the best that you can do is to talk about "digits". The number "1" is one digit; "1234" is four.
In a regular expression, you can match one or more of the preceding pattern using "+", so the regex "something[0-9]+x" should do what you want. If you want an upper bound on the number of digits, than you can try something like "something[0-9]{1,5}x"
Yes, simply use *, so in your example string.matches("something[0-9]+x")
It would match a string something followed by any digit from 0 to 9, which have to occur at least one time, so * means zero or more times, while + means it have to occur at least one time but can occur more times if it wants.
If you do [0-9]{n,m} you can specify with m and n in which range it can occur for example:
[0-9]{2,3} will match any digit and it have to occur 2 or 3 times, if you only use one digit in this bracs [0-9]{2} it has to occur at least 2 times.
But at last: simply learn to use google ... there are so many regexp sites with tutorials and stuff.
I have the user input a fraction problem in this format (4/8–3/12 or 3+2/3 or 12/16*4 or -2/3/64/96) and need to split up the two fractions into an array then I need to pull the numerator and denominator of both so I can simplify them in my other file and do whatever calculation it asks, so I need to use an array so i can call on the element that has the sign in it.
System.out.println("Enter Fraction:");
String answer = s.nextLine();
String[] numbers = answer.split(" ");
System.out.print(numbers);
Is there a way to split the array into int variables? I am lost. The solution might be simple but been at this project for 7 hours or so now.
You can split the input with a simple regular expression, like this:
Pattern p = Pattern.compile("\\d+|[+-/*]");
String inp = "2/3/64/96";
Matcher m = p.matcher(inp);
while (m.find()) {
System.out.println(m.group());
}
The heart of this solution is a regular expression:
\\d+|[+-/*]
\\d+ means "one or more digit; [+-/*] means "any of the +, -, /, or *".
Here is a demo on ideone.
Note that your program would need to be really careful about deciding if a + or a - is a unary "change sign" or a binary operation: when the sign is at the beginning or just after another operator, it's unary; otherwise, it's binary.
Eric Lippert has the best advice I've seen when dealing with these problems.
Write the problem out in english/pseudo code then start changing the pseudo code to real code.
Get Answer from user
Break Answer up to the 2 fractions/numbers and the math operator
Take the 2 different fractions and convert them to a double, float, whatever you need
Apply the math operation to the 2 numbers from the above step
Output the answer
Now just start replacing each line with how you would do that. I feel confident that you can probably figure out each step, but sometimes we get lost when thinking of the problem as a whole. If you can't figure out an individual step, post the code you have tried and we can help with a specific problem.
Your code so far just splits input into expressions. Your still need to split expressions into fractions.
When you already have String array of fractions you can split each element into 2 integers:
Split it into sub-strings with split("/")
Convert each sub-string to int with Integer.parseInt() method.
I want to create a regular expression in java using standard libraries that will accommodate the following sentence:
12 of 128
Obviously the numbers can be anything though... From 1 digit to many
Also, I'm not sure how to accommodate the word "of" but I thought maybe something along the lines of:
[\d\sof\s\d]
This should work for you:
(\d+\s+of\s+\d+)
This will assume that you want to capture the full block of text as "one group", and there can be one-or-more whitespace characters in between each (if only one space, you can change \s+ to just \s).
If you want to capture the numbers separately, you can try:
(\d+)\s+of\s+(\d+)
You want this:
\d+\sof\s\d+
The relevant change from what you already had is the addition of the two plus signs. That means, that it should match multiple digits but at least one.
Sample: http://regexr.com?32cao
This regexp
"\\d+ of \\d+"
will match at least one to any number of digits, followed by string " of " followed by one to any number of digits.