I am meeting a problem,just as follows:
{"_id":ObjectId("XXXXXXXX"),"phone":"123456"}
and now i want to query the document that the length of phone field is 5. I run the command as follows,
db.Phone.find({"phone":{"$regex":"\d{5}"}})
or
db.Phone.find({"phone":/\d{5}/})
they all do not Work. Could anybody help me figure out, how to use regex in mongo?
If you want to find docs where the phone number is exactly 5 digits, you need to anchor the regex to the start and end of the string with ^ and $:
db.Phone.find({phone: /^\d{5}$/})
Otherwise it will match any string that contains at least 5 digits in a row, anywhere in the string.
Related
So I have a bunch of labels submitted from my view which can range from Question1Label to Question10Label. I would like to create a regex which can validate that it follows the following format.
Question
Number
Label
Does anyone know how to create a regex which can do this?
I have this so far:
^Question[0-20]Label*$
Question[1-9][0-9]*Label will work for 1-99
If you want to just have 1-20 the following should work too:
Question([1-9]|1[0-9]*|20)Label
This basically means we have have 1-9 or a 1 followed by 0-9 or 20.
Lets say I have a string something-123.
I need to get last 5 (or less) characters of it but only up to - if there is one in the string, so the result would be like thing, but if string has no - in it, like something123 then the result would be ng123, and if string is like 123 then the result would be 123.
I know how to mach last 5 characters:
/.{5}$/
I know how to mach everything up to first -:
/[^-]*/
But I can not figure out how to combine them, and to make things worse I need to get the match without extracting it from specific groups and similar advanced regex stuff because I want to use it in SQL Anywhere, please help.
Tank you all for the help, but looks like a complete regex solution is going to be too complicated for my problem, so I did it very simple: SELECT right(regexp_substr('something-123', '[^-]*'), 4).
One option is to group the result:
(.{4})-
Now you have captured the result but without the -.
Or using lookarounds you can:
.{4}(?=-)
which matches any 4 characters that appears before "-".
You can use:
.{5}(?=(?:-[^-]*)?$)
See the regex demo
We match 5 symbols other than a newline only before the last - in the string or at the very end of the string ((?=(?:-[^-]*)?$)). You only need to collect the matches, no need checking groups/submatches.
UPDATE
To match any 1 to 5 characters other than a hyphen before the first hyphen (if present in the string), you can use
([^-]{1,5})(?:(?:-[^-]*)*)?$
See demo. We rely on a lookahead here, that checks if there are -+non-hyphen sequences are after the expected substring.
An faster alternative:
^[^-]*?([^-]{1,5})(?:-|$)
This regex will search for any characters other than - up to 1 to 5 such characters.
Note that here, the value we need is in Group 1.
How about:
(.{5})(?:-[^-]+)?$
The result is in group 1
Try this regex:
(.{1,5})(?:-.*|$)
Group 1 has the result you need
demo
I'm trying to use angular's ng-pattern attribute to enforce that the text in an input box matches a particular regular expression. I'm doing this for form validation. The input I'm using this on is a phone field. The phone field can accept 10 digit phone numbers but I don't want to require a specific format from my users other than it must contain a 10 digit number in there somewhere. So these would both be valid.
555-555-5555
(555) 555-5555
On the backend, I would just remove all formatting and store the raw digits. In the past for this what I've done is to remove all non-numeric characters and then just made sure the length == 10. It's simple enough to do in a couple of lines of code, but is it something regular expressions can handle? This isn't something I've ever tried to make a regular expression do. I don't want to support specific formats because I don't care if they accidentally enter an extra space or if they want to type their phone number like this: 55-55-55-55-55. It really doesn't matter to me, I just want the regex to match if there are 10 digits and no more somewhere in the string.
Thanks for the help guys!
To allow just 10 digits in a string with a regex, no more no less, you can use
^(?:\D*\d){10}\D*$
See demo (\D replaced with [^\d\n] to exclude newline for demo only).
The (\d\D*?){10} regex will find the match in a string with more than 10 digits.
^(?!(?:.*?\d){11,})[^a-zA-Z\n]+$
Try this.See demo.
https://regex101.com/r/iQ4nW0/1
You can make attribute type directive for it and set validity with dynamic regular expression.
I need to disallow combinations in this structure:
start by small "u"
the 5 following characters can not be numbers within (if starts by "u")
except this disallowed combination, allow only [a-zA-Z0-9]+
I did only regex like ^[^u][^0-9][^0-9][^0-9][^0-9][^0-9]$, because I have no idea for add only except for starting by "u".
List of some allowed combinations:
u12adfw3
u1a234
ud1235
And list of disallowed combinations:
u12345
u91
u1
I need this for aliases for system-generated name like "u20". Because I am creating system, when user can be identified by name/alias/e-mail (just looking for that string in database) and because user do not must set own alias, I want get there some limits. The destination of this regex is "pattern" in input tag in HTML or PHP check after submit.
If you have some interesting tutorials to do that/topics with simiplar problem or you just want help me, thanks you in advance :)
Greetings
If you're checking that in PHP, you could use preg_match and check with this regex:
^(?!u\d{1,5}\b)
preg_match will return false if the string begins with a u and 1 to 5 digits.
^ matches at the beginning of the string.
(?! ... ) is a negative lookahead. If what's inside matches, the whole regex will fail.
u\d{1,5} is to match u followed by 1 to 5 digits.
\b is a word boundary and will prevent any following word characters.
we Have an issue with spammers and I'd like to add a validation regex to the phone field in my form, in order to don't allow input which starts with a particular sequence of numbers.
I am using a wordpress plugin to build up the form, and I can add custom regex validation to each field.
so at the moment for my phone field I am using a text field and I have this regex to allow only numbers: /^\d+$/
the prefixes I'd like to block are these:
+44704, +44714, 0704, 0714, 0044704, 0044714
is it possible to create a regex which will check if the input starts with one of these sequences, and if yes it will block them?
If possible I need it to keep allowing only numbers, in addition of allowing only if it's not starting with one of those sequences.
I hope someone will be able to help me, as I really don't understand regex at all.. :(
Thank You!
You can make use of optional groups, like this:
^\+?(?:(?:00)?44|0)7[01]4
regex101 demo
This regex matches only strings that begins with the patterns you described. To negate it, you could use a negative lookahead with the pattern above:
^(?!\+?(?:(?:00)?44|0)7[01]4)
^ matches the beginning of the line
\+? matches an optional + sign.
(?:(?:00)?44|0) matches either of: 0044, or 44, or 0
7[01]4 matches either 704 or 714.
To validate the whole entry string and prevent the matches, then add the bit you already had, with an optional + sign:
/^(?!\+?((00)?44|0)7[01]4)\+?\d+$/