Validation using regex in java [closed] - java

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 7 years ago.
Improve this question
1)I have a field with the value as 11/06-13 (YY/DD-MM) how can i validate this value using regex.
2)I have a field with value 08:00 how can i validate this value using regex.
3)I have a field with value 2015-12-02+03:00 (YYYY-MM-DD+hh:mm) how can i validate this value using regex.
Please help i am new to regex.

Just read the Javadoc about Regular Expressions.
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
You can use
Pattern.matches(regex, Input);
to validate your Strings.
For your first Point it would be:
Pattern.matches("[0-9]{2}/[0-3][0-9]-(0|1)[0-9]", date);
The year can range from 00 to 99 followed by a "/". The day can range from 00 to 39. If you want to check for a valid day you can use:
(0[1-9])|((1|2)[0-9])|(30|1)
Now the day must be between 01 and 31.
The month can range from 00 to 19 in my first example. To avoid that you can use:
(0[1-9])|(1[0-2])
Complete Regex:
[0-9]{2}/((0[1-9])|((1|2)[0-9])|(30|1))-((0[1-9])|(1[0-2]))

Related

Regex to get the last value [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 3 years ago.
Improve this question
From the following value
[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]
I want to get the last number 2196879032
Some times value will be -1
[AAMP-PLAYER]aamp pos: [14..187..202..-1]
Instead of split method how can i extract last digit using regex method
You could match the last (possibly negative) number at the end of the string, just before the closing ]:
(\-?\d+)\]$
A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
(\-?\d+)(?!.*\d)
We could try using a String#replaceAll option here:
String input = "[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]";
String num = input.replaceAll("^.*\\.\\.(-?\\d+).*$", "$1");
System.out.println(num);
This prints:
2196879032

How to generate unique alphanumeric ID 11 characters long 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 3 years ago.
Improve this question
I need to work on a framework for Unique ID generation. Currently, a reference number is 11 characters with 5 characters Julian date, 6th character specific to a datacenter and other 5 - uniquely generated sequence of alpha characters. This make the algorithm to generate 20 million unique records.
I do not want UUID format. Need a more readable format potentially with composition of a date that can represent when it's generated and uniquely generated characters/numbers.
Just wanted to go over the potential algorithms.
Sorry, unlikely to be very unique.
UUID is unbeatable but verbose (database/java). It gives 128 bits. Encode those with URL-safe Base64 and you get 22 almost alphanumeric chars (with two extras like - _).
Rolling your own: System.nanos().

Regular Expression for Simple Maths Equation parser in Java [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 5 years ago.
Improve this question
Currently learning Java, and trying to parse a maths equation for valid inputs:
For example, the user has to input integers in the form:
Operand Operator Operand
in that specific order, and the program would then need to be able to tell if the inputs were in that form, and then work out the simple equation.
An example would be:
4 * 8 which the result would give as 32
the program would also reject something like 45.6 * 0.3, or 45 + 3 / 4
For this to work, do I have to use regular expression, or some other method of if loops?
You can use RegEx to on one hand, get rid of the invalid input, and on the other hand make use of the groups to extract the operands and operation and apply the math
/^(\\d+)\\s*([+\\-*\\/])\\s*(\\d+)$/
Edit:
As Rory Daulton pointed out, the signed integers are excluded from the above RegEx, so the below one should be used instead
/^([+\\-]?\\d+)\\s*([+\\-*\\/])\\s*([+\\-]?\\d+)$/
Take look that:
https://stackoverflow.com/a/11009403/8701820
^([-+/*]\d+(\.\d+)?)*
Also you can check this website: https://regex101.com

Remove Known Part of String Java [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 5 years ago.
Improve this question
A little help please. I want to strip-off a portion of my string which is made up of a number in Java.
eg. Have the following Strings - 60067 ; 600567 and 600876 and '600676600'
How do I remove 600 from each of the strings in Java so I remain with
67 ; 567 and 876 and '676600'
As Pavneet said in his comment, this works well:
"60067".substring(3);
Also, if you want to make sure it is the string "600" that you are removing:
"60067".replaceFirst("600", "");
Java Strings have no remove method, so it's common to replace the String with a blank one, "".
try this:
String s = "60067 ; 600567 and 600876";
s = s.replaceAll("600", "");
System.out.println(s);
I got
67 ; 567 and 876

I need regular expression in java to get text(along with first date) between dates [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 need regular expression in java to get text(along with start date) between dates
note:content in multiple lines
my text is in multiple lines like below:
05/22/2014 03:22:39.288 ffff gggg kkkkkk lllllll ssss kbhfg kjjkfjkne
kjjj
jhgdj6 saghjajdg hjhjdsjsa fkejfkw
05/22/2014 03:22:39.288 oooooo ppppp qqqq rrrrrr sss hkwhfwe fewhjgjw
hew jggjg khj
h gjfgw jgjs gad6jgj hdasggs hjhjwefj ewhrew
05/22/2014 03:22:39.378 mmmmmm nnn oooo
dsa6j gdsaja sgdjhs agj kbewbf hwe khhwejh
if i have input:
05/22/2014 03:22:39.288 ffff gggg kkkkkk 05/22/2014 lllllll
ssss kbhfg kjjkfjkne kjjj jhhjd jjhg jhhjgj
it should not highlight upto alone date it must highlight in between date time content along with start date time.
You could use this:
(?s)[0-9]{2}/[0-9]{2}/[0-9]{4}.*?(?=\s*[0-9]{2}/[0-9]{2}/[0-9]{4}|$)
See demo.

Categories

Resources