I'm trying to write a regex to validate a specific format. Here is it:
key=0(or)1;key=0(or)1;(repeated-or-not);key=0(or)1.
Or said otherwise:
\w
sign
0 or 1
; sign only between elements. Not on the last element.
All of previous repeated, or not.
The specificity is that the string can not end with a ";".
For now I've this ^(?:[a-z]+=[01];?)+(?<!;)$ which is right but not completely. since foo=1;bar=0foo=0;bar=1passes but even tough this part bar=0foo=0is incorrect.
Here are my current regex and some testing strings: https://regex101.com/r/lX0xT7/1
Thank you for your help,
Cheers,
You can use this regex:
^\w+=[01](?:;\w+=[01])*$
Updated RegEx Demo
Modifying your original regex-
^(?:[a-z]+=[01];)*(?:[a-z]+=[01])$
Demo
Related
Let's say I have these three names
John Doe (p45643)
Le'anne Frank
Molly-Mae Edwards
I want to match
1) John Doe
2) Le'anne Frank
3) Molly-Mae Edwards
The regex I have tried is
(^[a-zA-Z-'^\d]$)+
but it isn't working as I am expecting.
I would like help creating a regex pattern that:
Matches a name from start to finish, and cannot contain a number. The only permitted values each "name" can contain is, [a-zA-Z'-], so if a name was
J0hn then it shouldn't match
If I understood correctly your question, then you have a minor errors in your regex:
(^[a-zA-Z-'^\d]$)+
^-------^------Here
The - pointed above should be escaped or moved to the end since it works as a range character. The + is marking the group as repeated.
You can use this regex instead (following your previous pattern):
(^[a-zA-Z'^\d -]+$)
Regex demo
Update: for your comment. If you want to match separately, then you can use:
(\b[a-zA-Z'^\d-]+\b)
Regex demo
And if you only want to match string (not numbers), then you can use:
(\b[a-zA-Z'-]+\b)
Regex demo
You are using the anchors incorrectly. Based on the modifier it can match the whole string or a single line.
Try
/^[a-zA-Z'-]+$/
Thanks to #Djory Krache
The query I was looking for was
(\b[a-zA-Z'-]+\b)
I need to use regex match expression to find part of a file name.
eg file name is ABC01-001-M-001_0.dwg
I need to match the first digit after the underscore (this is the revision number of the drawing file name)
In this case with the example it would match the 0
Can anyone please advise the regex for this?
Many thanks.
pushpraj answer is almost correct. I take it you dont want to see the underscore.
so (?<=_)(\d+) would be the correct choice. The (?<=_) says, that the underscore have to be in front of your desired pattern but not included.
Demo: https://regex101.com/r/kH1nE2/2
I think this should be pretty simple.
_(\d)
online demo https://regex101.com/r/kH1nE2/1
in case you expect more digits you can use the following
_(\d+)
note "+" which will match 1 or more digits after underscore
In this case regex is not even needed.
$n = "ABC01-001-M-001_0.dwg";
Echo intval(explode("_", $n)[1]); // 0
https://3v4l.org/fBXc5
It seems you are looking for a javascript solution?
var n = "ABC01-001-M-001_0.dwg";
var int = parseInt(n.split("_")[1]);
Not sure it actually works because I'm not doing writing in javascript but ^^ is what I could google about the functions.
Maybe works, maybe not.
It seems to work :-)
https://jsfiddle.net/w9x7d40f/
I want a regex which return me only characters before first point.
Ex :
T420_02.DOMAIN.LOCAL
I want only T420_02
Please help me.
You can use the following regex: ^(.*?)(?=\.)
The captured group contains what you need (T420_02 in your example).
This simple expression should do what you need, assuming you want to match it at the beginning of the string:
^(.+?)\.
The capture group contains the string before (but not including) the ..
Here's a fiddle: http://www.rexfiddle.net/s8l0bn3
Use regex pattern ^[^.]+(?=[.])
I'm trying to select a substring using regex and I'm going round in circles. I need to select everything before the first "_".
exampale URL - GI_2013_JUNE_10_VOL3_LASTCHANCE
So the result Im looking for from the URL above would be "GI". The text before the first "_" can vary in length.
Any help would be much apprecited
The regex would be:
^[^_]+
and grab the whole regex match. But as a comment says, using a substring function is more efficient!
^[^_]*
...is the expression you're looking for.
It basically says: Select everything that is not an underscore, starting at the beginning of the string.
http://regexr.com?356in
Can someone help me create this regex. I need it to check to see if the string is either entirely whitespace(empty) or if it only contains positive whole numbers. If anything else it fails. This is what I have so far.
/^\s*|[0-9][0-9]*/
You're looking for:
/^(\s*|\d+)$/
If you want a positive number without leading zeros, use [1-9][0-9]*
If you don't care about whitespaces around the number, you can also try:
/^\s*\d*\s*$/
Note that you don't want to allow partial matching, for example 123abc, so you need the start and end anchors: ^...$.
Your regex has a common mistake: ^\s*|\d+$, for example, does not enforce a whole match, as is it the same as (^\s*)|(\d+$), reading, Spaces at the start, or digits at the end.
To match a number or empty string '' i.e the user has not entered any input do this
(^[0-9]+$|^$)
To match a number, an empty string, or a white space character
(^[0-9]+$|^$|^\s$)
Test this on regex101
Kobi has a good answer but technically you don't have to capture it (unless you're going to do something the output)
/^[\s\d]+$/
Or if you don't care if the string is completely empty (i.e. "")
/^[\s\d]*$/
To clarify I understood the original question to mean whitespace in the string should be ignored.
You can try it-
/^\d*$/
To match with white space-
/^[\s\d\s]*$/
This helped me. It matches empty string and any number you enter.
/^(|\d)+$/
Try here if you want: regex101.com
^\d+([\.\,][0]{2})?$
I found this worked for me. Allow any whole number, but only a decimal of .00
Pass
999 90
100
100.00
101.00
1000.00
Fails
101.01
1000.99
Try it at http://regexpal.com/