Mongo regex for "not match" or inverse [duplicate] - regex

This question already has answers here:
How can I use 'Not Like' operator in MongoDB
(2 answers)
Closed 2 years ago.
My mongo documents all contain a field called templateName. There are a few documents that contain the value: a_SystemDefaultTemplate, b_SystemDefaultTemplate, c_SystemDefaultTemplate etc.
I would like to find those documents whose templateName does not end with (or contain) SystemDefaultTemplate
I know it can be done using the $not operator like so:
db.collection.find({templateName: {$not: /.*SystemDefaultTemplate$/}})
But how do I do the same using regex?
I have tried the below but it does not seem to work.
db.collection.find({templateName: {$regex: "^(.*SystemDefaultTemplate$)"}})

try with negative look ahead ( meaning it should not contain the mentioned phrase)
db.collection.find({templateName: {$regex: "^(?!SystemDefaultTemplate$)"}})
?! is negative look ahead. And here is some explanation about it from http://rexegg.com/regex-disambiguation.html#lookarounds
"Negative Lookahead After the Match: \d+(?!\d| dollars)
Sample Match: 100 in 100 pesos
Explanation: \d+ matches 100, then the negative lookahead (?!\d| dollars) asserts that at that position in the string, what immediately follows is neither a digit nor the characters " dollars"
Negative Lookahead Before the Match: (?!\d+ dollars)\d+
Sample Match: 100 in 100 pesos
Explanation: The negative lookahead (?!\d+ dollars) asserts that at the current position in the string, what follows is not digits then the characters " dollars". If the assertion succeeds, the engine matches the digits with \d+."

Related

Is it possible to use regular expressions to find a string that could have a stray space any place in it? [duplicate]

This question already has answers here:
How to ignore whitespace in a regular expression subject string?
(7 answers)
Closed 11 days ago.
I haven't seen an answer to this specific question anywhere. My apologies if someone identifies it as a dupe. What I am wondering is: is it possible to search for:
abcdefghijk
matching any of the following:
a bcdefghijk
ab cdefghijk
abc defghijk
abcd efghijk
abcde fghijk
abcdef ghijk
abcdefg hijk
abcdefgh ijk
abcdefghi jk
abcdefghij k
I.e. I know the string I want to find, but it can end up with a stray space at any place.
Seems like this may be out of scope with regex, but wanted to be sure.
One approach would be to use a positive lookahead to find a space combined with a negative lookahead to not find a space.
Then let every character be followed by an optional space.
^(?=.* (?!.* ))?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?$
(?= - positive lookahead start
.* - anything followed by space
(?! - negative lookahead start
.* - anything followed by a space
) - negative lookahead end
) - positive lookahead end
? - 0 or 1 match
Demo

Need Regex to validate 11-digit phone number without plus sign [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 2 years ago.
I need a regex to validate phone number without plus (+) sign for example
46123456789,46-123-456-789,46-123-456-789
number should be 11 digit rest of should ignore
i am currently using this Regex /([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}/g
its not correct at all
About the pattern you tried:
Using this part in your pattern [+]? optionally matches a plus sign. It is wrapped in an optional group ([+]?\d{1,2}[.-\s]?)? possibly also matching 12 digits in total.
The character class [.-\s] matches 1 of the listed characters, allowing for mixed delimiters like 333-333.3333
You are not using anchors, and can also possible get partial matches.
You could use an alternation | to match either the pattern with the hyphens and digits or match only 11 digits.
^(?:\d{2}-\d{3}-\d{3}-\d{3}|\d{11})$
^ Start of string
(?: Non capture group for the alternation
\d{2}-\d{3}-\d{3}-\d{3} Match either the number of digits separated by a hyphen
| Or
\d{11} Match 11 digits
) Close group
$ End of string.
Regex demo
If you want multiple delimiters which have to be consistent, you could use a capturing group with a backreference \1
^(?:\d{2}([-.])\d{3}\1\d{3}\1\d{3}|\d{11})$
Regex demo
I would have this function return true or false and use as is.
function isPhoneValid(phone) {
let onlyNumbers = phone.replace(/[^0-9]/g, "");
if (onlyNumbers.length != 11) console.log(phone + ' is invalid');
else console.log(phone + ' is valid');
}
isPhoneValid('1 (888) 555-1234');
isPhoneValid('(888) 555-1234');
I am not sure how is the input looks like. But based on your question I supposed you want to trim it and match it with regex?
trim your input.
string.split(/[^0-9.]/).join('');
and you can match it with this regex:
((\([0-9]{3}\))|[0-9]{3})[\s\-]?[\0-9]{3}[\s\-]?[0-9]{4}$

Regex for matching numbers with optional decimal digits [duplicate]

This question already has answers here:
Regular expression for floating point numbers
(20 answers)
Closed 3 years ago.
I am trying to extract rating from a tweet using regular expression. For example for below tweet, I want to get the user rating(9.75) and maximum rating(10).
This is Logan, the Chow who lived. He solemnly swears he's up to lots of good. 9.75/10
I used below regex, but the capture groups 1 and 2 has results 75 and 10. I am not sure why the user rating is captured only after decimal group.
.*(\d+\.?\d+)\/(\d*\.?\d*)
If you want both numbers to have optional decimal you should place the match one or ore + and the match zero or more * on the correct places, where they match the mandatory leading digit and then the optional decimals
(\d+\.?\d+)\/(\d*\.?\d*)
with
(\d+\.?\d*)\/(\d+\.?\d*)
This will match at least one digit followed by maybe a dot and then again maybe some more digits.
Live link: https://regex101.com/r/qc5Zwz/1
\b(\d+(?:\.\d+)?)\/(\d+)\b
\b - expect a word boundary (eg, space, non-letter character)
( - start capturing the 'rating'
\d+ - integer part
(?:\.\d+)? - wrap the decimal part, don’t capture it as a group; make it optional
) - end of 'rating' capturing group
\/- expect a forward slash
(\d+) - capture the 'maximum'
\b - expect a word boundary again
const text = 'This is Logan, the Chow who lived. He solemnly swears he\'s up to lots of good. 9.75/10'
const pattern = /\b(\d+(?:\.\d+)?)\/(\d+)\b/
console.log(text.match(pattern))
https://regex101.com/r/foO1DF/2

REGEX: match all numbers if a keyword exists [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to find a regex which searches for every numbers if a keyword exists in the paragraph.
For example, if my keyword is something, with this paragraph:
20
30
abc
40
def
something
my regex should get 20, 30 and 40. But for this one:
50
60
xyz
it should get nothing.
Can you guys help me out to find a good regex. Thank you so much! I'm using PCRE
You can use this regex in single line (DOTALL) mode with a lookahead assertion:
(?s)\b\d+(?=.*\bsomething\b)
It will match numbers only when there is a word something ahead in input.
RegEx Demo
RegEx Details:
(?s): Enable single line mode so that dot also matches newlines
\b: Match a word boundary
\d+: Match 1+ digits
(?=.*\bsomething\b): Positive lookahead to assert that we have a word something ahead of us from current position

Regex to match given amount of characters in undefined order [duplicate]

This question already has answers here:
Regex to match exactly n occurrences of letters and m occurrences of digits
(3 answers)
Closed 4 years ago.
I am looking for a regex that matches the following:
2 times the character 'a' and 3 times the character 'b'.
Additionally, the characters do not have to be subsequent, meaning that not only 'aabbb' and 'bbaaa' should be allowed, but also 'ababb', 'abbab' and so forth.
By the sound of it this should be an easy task, but atm I just can't wrap my head around it. Redirection to a good read is appreciated.
You need to use positive lookaheads. This is the same as the password validation problem described here.
Edit:
A positive lookahed will allow you to check a pattern against the string without changing where the next part of the regex matches. This means that you can test multiple regex patterns at the current position of the string and for the regex to match all the positive lookaheads will have to match.
In your case you are looking for 2 a' and 3 b's so the regex to match exactly 2 a's anywhere in the string is /^[^a]*a[^a]*a[^a]*$/ and for 3 b's is /^[^b]*b[^b]*b[^b]*b[^b]*$/ we now need to combine these so that we can match both together as follows /^(?=[^a]*a[^a]*a[^a]*$)(?=[^b]*b[^b]*b[^b]*b[^b]*$).*$/. This will start at the beginning of the string with the ^ anchor, then look for exactly 2 a's then the end of the string. Then because that was a positive lookahead the (?= ... ) the position for the next part of the pattern to match at in the string wont move so we are still at the start of the string and now match exactly 3 b's. As this is a positive lookahead we are still at the beginning of the string but now know that we have 2 a's and 3'b in the string so we match the whole of the string with .*$.