Regular expression for format XXXXXXX_YZZZZ - java

I am trying to write a regular expression in java which will validate following format-
XXXXXXXX_YZZZZ
where
X – alphanumeric characters(8 characters)
Y - alpha character
Z - numeric characters
what I have tried for first part is - ^[a-zA-Z0-9 ]*$ but I am not getting how to go for second part.
Can any one tell me what will be the correct regex for required format ?

You forgot to specify the amount and the underscore I assume...
/^[a-z0-9]{8}_[a-z][0-9]{4}$/i

Look at JavaDoc, then you can translate your requirements to:
"^\\p{Alnum}{8}_\\p{Alpha}\\p{Digit}{4}$"
It uses predefined character classes, like you listed in your question.

How about this?
^[a-ZA-Z0-9]{8}\_[a-zA-Z][0-9]{4}$
You can also group the results:
^([a-ZA-Z0-9]{8})\_([a-zA-Z])([0-9]{4})$
so that you can address the X, Y and Z parts individually from the results.

Try this regex:
^[A-Za-z\d]{8}_[A-Za-z]\d{4}$
Your regex matches zero or more alphanumeric characters and/or whitespaces.
This is a good place to learn regex : http://www.regular-expressions.info

Try this regular expression
^[a-zA-Z0-9]{8}[_][a-zA-Z][0-9]{4}$

Try:
^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$
Regexper is your friend here.

^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$

In Java, you can use metacharacters to express regulars expressions :
"8abba778_a2012".matches("^\\w{8}_[a-z]\\d{4}$");
[EDIT] : According #Jon Dvorak, I am correcting my answer. In fact, \w is too generous and also applies to the underscore character _. The correct answer :
"8abba778_a2012".matches("^[a-zA-Z0-9]{8}_[a-z]\\d{4}$");

Related

Add Dash to Java Regex

I am trying to modify an existing Regex expression being pulled in from a properties file from a Java program that someone else built.
The current Regex expression used to match an email address is -
RR.emailRegex=^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
That matches email addresses such as abc.xyz#example.com, but now some email addresses have dashes in them such as abc-def.xyz#example.com and those are failing the Regex pattern match.
What would my new Regex expression be to add the dash to that regular expression match or is there a better way to represent that?
Basing on the regex you are using, you can add the dash into your character class:
RR.emailRegex=^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
add
RR.emailRegex=^[a-zA-Z0-9_\\.-]+#[a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+$
Btw, you can shorten your regex like this:
RR.emailRegex=^[\\w.-]+#[\\w-]+\\.[\\w-]+$
Anyway, I would use Apache EmailValidator instead like this:
if (EmailValidator.getInstance().isValid(email)) ....
Meaning of - inside a character class is different than used elsewhere. Inside character class - denotes range. e.g. 0-9. If you want to include -, write it in beginning or ending of character class like [-0-9] or [0-9-].
You also don't need to escape . inside character class because it is treated as . literally inside character class.
Your regex can be simplified further. \w denotes [A-Za-z0-9_]. So you can use
^[-\w.]+#[\w]+\.[\w]+$
In Java, this can be written as
^[-\\w.]+#[\\w]+\\.[\\w]+$
^[a-zA-Z0-9_\\.\\-]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
Should solve your problem. In regex you need to escape anything that has meaning in the Regex engine (eg. -, ?, *, etc.).
The correct Regex fix is below.
OLD Regex Expression
^[a-zA-Z0-9_\\.]+#[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+$
NEW Regex Expression
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
Actually I read this post it covers all special cases, so the best one that's work correctly with java is
String pattern ="(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

Regex to match slash does not work

I want a regex to match these:
/web/search/abc
/web/search/employee/999999999
/web/search/employee/78524152
But not this:
/web/search/employee/123456789
I wrote the following regex for Java, but it does not seem to be working.
/web/search/(?!/(employee/123456789)).*
Can someone tell me the correct regex to do this?
This is because you double the / in the lookahead. Try with:
/web/search/(?!employee/123456789$).*
What you tried is : /web/search/(?!/(employee/123456789)) can be represented as
You need to change it as /web/search/(?!employee/123456789) can be represented as
I would say
str.contains("^/web/search/(?!employee/123456789)")
satisfies your requirements.
Online demonstration here: http://regex101.com/r/dF1eU9
Try the following:
/web/search/(?!(employee/123456789$)).*
The slash was doubled in the negating look-ahead group.

form java regex to php regex

I have a regular expression in Java: [^a-zA-Z0-9.-_]
How to form this regular expression from java to php?
In php (PCRE) this regular expression looks like
[^a-zA-Z0-9.-_]
Yep, it's exactly the same
it is the same for this particular regex.
But you can make shorter with:
[^\w.-]
and don't forget that the - character must be placed at the last position in a character class
It's exactly the same but you might need to put delimiters around it, e.g. parenthesis:
([^a-zA-Z0-9._-])
See the little difference moving the minus to the end. That is because [.-_] matches ./0...9:;<=>?#A...Z[\]^_. I guess you're not looking for the negation of this as you had already 0-9 and A-Z covered.

How do I use a regular expression to match an eight-digit number that can also have a space in the middle?

I want to match 1234 5678 or 12345678
Is this regular expression wrong?
if(!number.matches("^\\d{4}[, ]\\d{4}$")){
throw new Exception(" number is not valid : "+number);
}
try a quantifier after the []
^\d{4}[\s,]?\d{4}$
You are close, you need to specify that the space/comma is optional with a quantifier. ? is a good one because it means "zero or one". So your expression would be
^\d{4}[, ]?\d{4}$
Do you want to match the comma as well? [, ] matches a comma or a space char. The effect you're going for looks like ( |), except there are better ways to do it:
I think what you're looking for is:
/^\d{4}\s?\d{4}$/
Note that the \s can match any space char, including newlines. If you only want to match the space char ' ', then use the following:
/^\d{4}[ ]?\d{4}$/
there is modifcation required to match '12345678'
use this regular expression : "^\d{4}[, ]?\d{4}$"
Hope this helps
^\d{4}[\s]?\d{4}$
Try this without the comma.
Perhaps this is a good time to bring up the online Regex interactive tutorial.
Great tool for playing around with Regex expressions without having to mess up your own code.

Simple regex required

I've never used regexes in my life and by jove it looks like a deep pool to dive into. Anyway,
I need a regex for this pattern (AN is alphanumeric (a-z or 0-9), N is numeric (0-9) and A is alphabetic (a-z)):
AN,AN,AN,AN,AN,N,N,N,N,N,N,AN,AN,AN,A,A
That's five AN's, followed by six N's, followed by three AN's, followed finally by two A's.
If it makes a difference, the language I'm using is Java.
[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}
should work in most RE dialects for the tasks as you specified it -- most of them will also support abbreviations such as \d (digit) in lieu of [0-9] (but if alphabetics need to be lowercase, as you appear to be requesting, you'll probably need to spell out the a-z parts).
Replace each AN by [a-z0-9], each N by [0-9], and each A by [a-z].
30 seconds in Expresso:
[a-zA-Z0-9]{5}[0-9]{6}[a-zA-Z0-9]{3}[0-9]{2}
Case insensitive, but you can probably define that in Java instead of the regex.
For the example you posted, the following should work fine.
(([A-Za-z\d])*,){5}+(([\d])*,){6}+(([A-Za-z\d])*,){3}+([\d])*,[\d]*
In Java you should be able use it like this:
boolean foundMatch = subjectString.matches("(([A-Za-z\\d])*,){5}+(([\\d])*,){6}+(([A-Za-z\\d])*,){3}+([\\d])*,[\\d]*");
I used, this tool to help in learning RegEx, it also make this really easy.
http://www.regexbuddy.com/
Try looking at some simple java regex tutorials such as this
They'll tell you how you form regular expressions and also how to use it in java.
This should match the pattern you request.
[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}
In addition, you could add Beginning of String / End of String matches, if your string match should fail if any other chars are in it:
^[a-z0-9]{5}[0-9]{6}[a-z0-9]{3}[a-z]{2}$

Categories