I know how to validate a constant string with regex, but I'm really having trouble finding out how to do the following: I want a regex to validate the string edv_ after that I want the validation to be dependent:
if the user inputs for example edv_2, only 6 or 7 can be the next character. So only edv_26 or edv_27 would be valid
if the user would enter edv_3 then only edv_32 or edv_39 would be valid
Ive tried searching on the internet watched several youtube tutorials. None of them seem to handle this kind of thing. It's always only 1 constant thing they want to validate.
/[e][d][v][_][A]/ig
This matches the first part (edv_digit) but I have no clue how I should continue with the if else conditions.
You basically need alternations for handling your various cases. You can use this regex which matches as per your criteria.
\bedv_(?:2[67]|3[29])\b
Here boundaries ensure it doesn't match partial text like abcedv_26 or edv_26111 and it starts matching with edv_ then looks for either 2 followed by either 6 or 7 or looks for 3 followed by 2 or 9.
Live Demo
Related
I am trying to come up with a smallest regex possible for extracting parts of a string with the last section being an optional one. The string will look something like:
jack:Bill(23):Space Force (23, Apple;Orange)
or
jack:Bill(23):Space Force
I need to extract as follows:
Jack
Bill(23)
Space Force
23
Apple;Orange
The last 2 items may or may not appear based on the source string. I am trying with a regex like:
(.*?):(.*?):(.*?)(\\(([0-9]+),([^\\)]*)?\\))?
But this does not seem to work.
I got it working with (.*?):(.*?):([^\\(]*)(\\(([0-9]+), ([^\\)]*)?\\))?
This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 7 years ago.
Lets say I have a bunch of mongodb records like so, which are all strings:
{myRecord:'foobarbazfoobaz'}
{myRecord:'bazbarfoobarbaz'}
{myRecord:'foobarfoofoobaz'}
{myRecord:'bazbarbazbazbar'}
I need to be able to partial string match in two ways:
1) I want to match on 'foobar' so it returns:
'foobarbazfoobaz'
'foobarfoofoobaz'
Note that here, 'foobar' is a partial string that is matched against any of the records from the beginning of the string. It doesn't matter if 'foobar' turns up later in the string. As long the first six characters of 'foobar' match against the the first six characters of the record, I want to get it back.
2) I need to be able match on 'baz%%%baz' so it returns:
bazbarbazbazbar
Here 'baz%%%baz' matches the first three characters of any of the records, ignores the next three, then matches against the final three. Again, it doesn't matter if this pattern occurs later in the string, I am just interested in if I can match it from the beginning of the string.
I think there is some kind mongo regex to do this (hopefully) but I am terrible when it comes to regex. Any help would be greatly appreciated.
This is for a web application where users are searching for sequences of events on a timeline and they will always have to search from the beginning, but can leave blanks in the search if they wish to.
You can try $regex operator
1) I want to match on 'foobar'
db.collection.find({"myRecord":{"$regex":"^foobar*"}})
I need to be able match on 'baz%%%baz'
db.collection.find({"myRecord":{"$regex":"^baz.{3}baz"}})
Hope it will help
Hang on - just found a way to deal with the second case, which turns out to be unexpectedly straightforward:
{"myRecord":{"$regex":"^baz.{3}.baz"}}
I probably should spend some time learning how to use regex!
I am pretty new to the concept of regex and so I am hoping an expert user can help me craft the right expression to find all the matches in a string. I have a string that represents a lot of support information in it for vulnerabilities data. In that string are a series of CVE references in the format: CVE-2015-4000. Can anyone provide me a sample regex on finding all occurrences of that ? obviously, the numeric part of that changes throughout the string...
Generally you should always include your previous efforts in your question, what exactly you expect to match, etc. But since I am aware of the format and this is an easy one...
CVE-\d{4}-\d{4,7}
This matches first CVE- then a 4-digit number for the year identifier and then a 4 to 7 digit number to identify the vulnerability as per the new standard.
See this in action here.
If you need an exact match without any syntax or logic violations, you can try this:
^(CVE-(1999|2\d{3})-(0\d{2}[1-9]|[1-9]\d{3,}))$
You can run this against the test data supplied by MITRE here to test your code or test it online here.
I will add my two cents to the accepted answer. Incase we want to detect case insensitive "CVE" we can following regex
r'(?i)\bcve\-\d{4}-\d{4,7}'
I need to match complete dynamic URL to set-up as a goal in Google Analytics. I don't know how to do that. I have searched on Google with no luck.
So here is the case.
When pressed enter button, the goal URL would be different depending on the product selected.
Example:
http://www.somesite.com/footwear/mens/hiking-boots/atmosphere-boot-p7023.aspx?cl=BLACK
http://www.somesite.com/womens/clothing/waterproof-jackets/canyon-womens-long-jacket-p7372.aspx?cl=KHAKI
http://www.somesite.com/travel/accessories/mosquito-nets/mosquito-net-double-p5549.aspx?cl=WHITE
http://www.somesite.com/ski/accessories/ski-socks-tubes/ski-socks-p2348.aspx?cl=BLACK
If you look closely in the URL, you can see that there are three parts:
http://www.somesite.com/{ 1st part }/{ 2nd part }/{3rd part }/{ page URL }/{ querystring param}
So if I manually change page URL part like p2348 to p1234, website will redirect to the proper page:
http://www.somesite.com/kids/clothing/padded-down-jackets/khuno-kids-padded-jacket-p1234.aspx?cl=BLUE
I don't know how to do that. Please help with regular expression to match those 4 digit while p remains there OR help me with those three parts matching any text/number and then 4 digit product code.
You should try this regex. It's the most simple one and functional as well.
p\d{4}
This will return you strings like p7634, p7351, p0872.
If you are not completely sure there will be exactly 4 digits, use the following regex.
p\d*
This one will return you strings like p43, p9165, p012, p456897689 and others.
Try
p[0-9][0-9][0-9][0-9]\.aspx
if there are always 4 digits after the p.
Your attempt
[^p]\d[0-9][0-9]
does not work because [^p] matches anything except for p, and \d[0-9][0-9] matches only three digits instead of four.
Hi I want to be able to set the regular expression to allow for dates to be entered like this
01/01/1900 or 01/01/70, I have the following but not sure how to make it so that it takes 4 or 2 at the end.
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$
The other one I would like to know is for URL
This one I have no idea how do I make it so that it matches correct URL's?
Thank you
This should match two our four digit numbers:
\d{2}(\d{2})?
Your full regex would be something like this:
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]\d{2}(\d{2})?$
URLs are hard to test. http://localhost is a valid URL and so it https://test.example.co.uk:443/index.ece?foo=bar. I would look for something in your language to test this for you or do a very simple test like this (you will have to delimit some special chars depending on the regex engine you use):
^https?://
To modify your regex so that it takes either 2 or 4 digits at the end, you can try this:
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}|[0-9]{2})$
For URLs, you can try (from here):
(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
or have a look at this S.O. question.
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}|[0-9]{2})$
Well, is ([0-9]{4}|[0-9]{2}) not good enough for you? Probably you could add some checking that first two digits in the four-digits group is 19 or 20 but it depends on your needs.
As for URL matching look here. There's many of them with tests.
You can use another alternation in at the end to accept 2 or 4 (the same way you do the "or" options for the other date parts). Alternatively, you can require 2 digits in the last position, and then have 2 optional digits after that.
Unless you need to capture the individual parts (day, month, year), you should use non-capturing parentheses, like this (?:) (that's the .NET syntax).
Finally, you should consider the type of validation that you are trying to achieve with this. It is probably better to enforce the format, and not worry about bad forms like 91/73/9004 because even with what you have you can still get invalid dates, like 02/31/2011. Since you probably have to perform further validation, why not simplify the regex to something like ^(?:\d{1,2}[-/.]){2}\d{2}(?:\d{2})?$
As for URLs, stackoverflow is littered with duplicate questions about this.