I'm looking to validate a chess FEN string and I'm working on the Regex for it. I'm looking to implement only very simple validation. Here are the rules I'm looking to match with my regex:
Exactly 7 "/" characters
Start and end of the string cannot be "/"
In between the slashes it must be either a number from 1-8 or the letters PNBRQK uppercase or lowercase
Example of a match
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
Examples of non-match
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR/
/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR/
rnbqkbnr/pppppppp/8/8/8/10/PPPPPPPP/RNBQKBNR
rnbqkbnr/Z/8/8/8/8/PPPPPPPP/RNBQKBNR
Currently, I have been able to implement exactly 7 "/" anywhere in the string with the following regex:
/^(?:[^\/]*\/){7}[^\/]*$/gm
I'm unsure how to implement the rest as RegEx is not my strong suit.
This should do the trick: (passes all your tests)
/^(?:(?:[PNBRQK]+|[1-8])\/){7}(?:[PNBRQK]+|[1-8])$/gim
All you needed was to use positive matching for the characters you're after instead of "not slash". The key addition is the non-capturing group with one or more PNBRQK or a digit from 1-8. The same group is repeated at the end of the expression.
Oh, and I added the i flag for case insensitive matching.
/^([1-8PNBRQK]+\/){7}[1-8PNBRQK]+$/gim
/gim = global, case insensitive, and multiline.
I got the above working on https://regexr.com/ - one of my favorite places for working out regex problems (but I know there are many other good resources online).
Hope this helps.
Related
I tried with many patters for username in my Angular5 application. But didn't get a suitable solution for my requirement.
The Rules are
Minimum 6 characters
Only numbers are not allowed at least one character should be there
No special characters allowed except _
No space allowed
Character only is allowed
I tried with /^[a-zA-Z0-9]+([_]?[a-zA-Z0-9])*$/
/^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![_\s\-]{6,}.*)$/
You can try this regex:
^[a-zA-Z0-9_]{5,}[a-zA-Z]+[0-9]*$
[a-zA-Z0-9_]{5,} to match at least five alphanumerics and the underscore
[a-zA-Z]+ to have at least one letter
[0-9]* to match zero to any occurrence of the given numbers range
Hope this helps.
You can use the following regex:
^(?=[a-z_\d]*[a-z])[a-z_\d]{6,}$
in case insensitive mode as tested on regex101: demo
Explanations:
^ anchor for the beginning of the string
$ anchor for the end of the string
(?=[a-z_\d]*[a-z]) to force the presence of at least one letter
[a-z_\d]{6,} implement the at least 6 char constraint
Yes this is fine for me. Thanks./^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![-?\d+\.?\d*$]{6,}.*)$/
I would like to be able to use new RegExp() in JS to match words like Macys to Macy's. Can someone show me how they would achieve this please. This is used for a search feature and i would like to return results if they the user types either spelling of the macys brand.
/macy'?s/gmi
macy matches the characters
macy
literally (case insensitive)
'?
matches the character ' literally
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed[greedy]
s
matches the character s literally (case insensitive)
g modifier:global.
Demo:
https://regex101.com/r/tV6yG1/1
PS: I'm using the stack android app and I cannot format the code as I'd like, but you get the idea of what's needed.
As #torazaburo pointed out: /Macy'?s/ is the regex you want. If you want it to be case-insensitive, add the i flag at the end of the regex.
/Macy'?s/i.test('Macys'); // true
/Macy'?s/i.test("Macy's"); // true
/Macy'?s/i.test("macys"); // true
so here is my problem: I'm checking an input of 2 years with a hyphen. Like:
2001-2015
To test this, I use the simple regex
/^([0-9]{4})-([0-9]{4})$/
I know groups aren't needed, and (19|20)[0-9]{2}, is a closer match to the basic year exp, but bear with me.
Now, if my requirement was to match the two years only if they are the same, i could have used a backreference like:
/^([0-9]{4})-\1$/
which matches 2000-2000 but not 2000-2014
My actual requirement is exactly the opposite. I want it to match if the years are different but not if they're same. That is, 2000-2014 should match. 2000-2000 should not.
And using the negative of the boolean I find is not an option. I need this for a huuuge regex which is supposed to match a whole lot of different date formats. This is just a part of it.
Is there any way to achieve this?
You can use a negative lookahead to achieve this:
^([0-9]{4})-(?!\1)[0-9]{4}$
Demo
This is almost the same pattern, except it inserts a condition check using the backreference.
(?!\1) will fail if \1 matches at its position.
You can use negative lookahead:
\b(\d{4})-(?!\1)\d{4}\b
RegEx Demo
Use Negative Lookahead.
Like this :
^([0-9]{4})-(?!\1)[0-9]{4}$
It does work on your example.
Explanation : (?!\1) Assert that it is impossible to match the regex \1. Then you just put your 4 digits requirement.
I am trying to define the regular expression required for my ASP.NET validator to run properly. Currently with the expression below I am able to properly validate the following sample string in firefox but not in IE
12{2}12{0-9}1{12,13}
using
(({\d+\})*|(\d)*|({(\d+,)+\d+\})*|({(\d+)\-(\d+)\})*)+
After doing some research it seems that this is due to the lookahead bug but since I am fairly new to using regex I do not understand how I can modify it properly to work around the bug?
Please feed me with higher knowledge!!
EDIT:
The expression must match these three optional individual component that can be in the string in any order. I tried to come up with an expression describing each individual component and then merging them into a single expression.
{n} regex {\d+\} to match sample {423} optional digits
{n,n,n} regex {(\d+,)+\d+\} to match sample set of digit {24,25,26}
{n-n} regex {(\d+)\-(\d+)\} to match sample range of {0-9}
individual digits (\d) to match sample 232
EDIT 2:
In the end I will be using this expression and a special thanks to woohoo
((\d*\#*\**)*\{((\d*\#*\**)+|(\d*\#*\**)+\-(\d*\#*\**)+|((\d*\#*\**)+\,)+(\d*\#*\**)+)\}(\d*\#*\**)*)+
the expression supports digits # and * at every position.
I'm afraid the regular expression you posted above has some errors, and it looks too complicated for what you try to achieve. I would do it this way:
\d+\{(\d+|\d+\-\d+|\d+\,\d+)\}
eventually you can add the + sign to match one or more of these,
(\d+\{(\d+|\d+\-\d+|\d+\,\d+)\})+
or, if you want to match a specific number of those, use {m,n} quantitative expression:
(\d+\{(\d+|\d+\-\d+|\d+\,\d+)\}){3,}
In this case I made it to match exactly 3 pieces.
I was just looking for a regex expression to check and see if both numbers and letters exist.
Just to clarify the query, the regex is going to be written in javascript and used to validate an address.
I would use a regular expression which matches any letter followed by any digit (with any possible characters in between) or digit then letter (with anything in between):
var hasNumbersAndLetters = function(str) {
var regex = /(?:[A-Za-z].*?\d|\d.*?[A-Za-z])/;
return !!str.match(regex);
};
Much easier to run two checks.
/\pL/ && /\pN/
To do both checks in one pattern, you need something like
/\pL.*\pN|\pN.*\pL/s
Languages supporting zero-width lookaheads can eliminate the redundancy:
/^(?=.*\pL/)(?=.*\pN/)/s ( or /^(?=.*\pL/).*\pN/s )
But it's harder to read.
Pardon me for not using JS's match function, but the question is really about regular expressions, and I'm not familiar with JS's match function.
if it is a single word you are matching without spaces, with both numbers and letters, it can be assumed they touch somewhere - so if it matches letter then number or number then letter we have a match - so:
([a-zA-Z][0-9]|[0-9][a-zA-Z])
Edit: where there may be spaces then you can use lookahead assertions like this
([a-zA-Z](?=.*[0-9])|[0-9](?=.*[a-zA-Z]))
Saw you wanted to validate an address
Removed by regex answer as javascript has no Unicode support, except for matching single characters http://www.regular-expressions.info/unicode.html
This should do it:
^[\w]*[^\W_][\w]*$