I have this pattern:
Pattern.compile("T([0-9]*)");
which works fine for positive numbers but I need it to also do negative numbers for instance "T-1T3T44" should work. Or maybe use space instead of 'T' so it should work for strings like this:"-1 2 3 2 -1 6 2". Sorry I haven't really used regular expressions before.So any suggestions? Thanks.
Pattern.compile("T(-{0,1}(?!0)\\d+)");
Please note the usage of negative look-ahead (?!0) to exclude -0 number and numbers that start with 0.
Have you thought of trying:
"T(-?[0-9]+)"
You'll notice I've also changed the "*" (zero or more) to "+" (one or more) since "" isn't technically a number :-)
Try with:
Pattern.compile("T(-?[0-9]*)");
Make the minus optional may be?
T-?([0-9]*)
Pattern.compile("T-?([0-9]*)");
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 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]
Guys I just started learning Regular Expression in java. I am trying to construct a regular expression for a number. It is ok for integers if I use "\d+".
But if there is any decimal number, it is obvious that the above RE won't work.
So I constructed a RE that is "\d+\.\d+" . It is ok for decimal numbers but
NOT FOR INTEGERS.
So I want to make a RE for BOTH INTEGERS and DECIMALS. Any idea is appreciated.
Make the part after the decimal point optional.
"\d+(?:\.\d+)?"
The leading ?: after the initial parenthesis is just to avoid creating a matching group.
You can try to use this regex:
/^\d*\.?\d*$/
The Javadoc for java.lang.Double#valueOf(String) gives a detailed but well-documented regular expression for how it parses numbers. You may find it instructive to read that and work out which parts are relevant to your problem.
Can we have a regex to detect if a number is even ?
I was wondering if we can have a regex to do this instead of usual % or bit operations.
Thanks for replies :)
You can try:
^-?\d*[02468]$
Explanation:
^ : Start anchor.
-? : Optional negative sign.
\d* : Zero or more digits.
[02468] : Char class to match a 0
or 2 or 4 or 6 or 8
$ : End anchor
Since the correct answer has already been given, I'll argue that regex would not be my first choice for this.
if the number fits the long range, use %
if it does not, you can use BigInteger.remainder(..), but perhaps checking whether the last char represents an even digit would be more efficient.
If it is a string, just check if endsWith(0) || endsWith(2) || .. returns true. If it is number, it is very simple.
Try this, I'm not sure if it's the same syntax in java:
^\d*(2|4|6|8|0)$
Sure, you just check if the last number is a 0/2/4/6/8
Never use regex for a job that can be easily done otherwise.
I came across this Microsoft blog that says the same: Link