Diango matching a string with a regex field - django

In django, if we have a regex and look for a rows whose a field is matching with a regex, it will be something like this:
MyAudience.objects.filter(email__iregex=f'^({'|'.join(emails)})$')
But what if I want to do it the other way?
Let's say I have a string:
email = 'abc#example.com'
and in my model AudienceFilter, I have a field pattern. This column keeps the regex for the model. I want to find with patterns do match my string? How can I do that?
AudienceFilter.objects.filter(pattern__?????=email)

You can inject the value as an alias, and then annotate:
from django.db.models import F, Value
AudienceFilter.objects.alias(email=Value(email)).filter(
email__iregex=F('pattern')
)

Related

How to compare two string using RegEx

I have a collection in the MongoDB which has list of URLs like below.
In my business logic for some requirements, I want to check whether the called URL is matching with any of the records in the DB records.
like req.originalUrl i get suppose
/logistics/initiator/5ee7a0be36acdc46ae0576d6/users
But in the above URL obviously, I'm getting the actual Id -- 5ee7a0be36acdc46ae0576d6
What i tried:
I tried manually concatinating the req.baseUrl and req.route.path but that still gives me the below string
/logistics/initiator/:initiator/users
which is again incomparable.
Replace the ID with \{\w+\}, and use that as a regular expression to match against the url column in the table. So the regexp should be /logistics/initiator/\{\w+\}/users.

Extract the fields in json log if the filed exists using regex

I am trying to extract the fields in the below sample data
{"Value":{"Data":{"Items":[{"FieldType":"ABC","Value":"*****"},{"FieldType":"ACB","Value":"*****"},{"FieldType":"ABCD","Value":"*****"},,{"FieldType":"XYZ","Value":"*****"}],"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}
{"Value":{"Data":{"Items":[{"FieldType":"ABC","Value":"*****"},{"FieldType":"XYZ","Value":"*****"},{"FieldType":"ACD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}
{"Value":{"Data":{"Items":[{"FieldType":"XYZ","Value":"*****"},{"FieldType":"ACD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}
{"Value":{"Data":{"Items":[{"FieldType":"ABCD","Value":"*****"}],,"EncryptedDocKey":"*****","Domain":"*****","Partner":"***","Carrier":"****"},"RequestTrackerId":"****","Message":"OK"},"Formatters":[],"ContentTypes":[],"DeclaredType":null,"StatusCode":null}
I need to extract the text next to FieldType (ABC,XYZ,ABCD,ACB,ACB ...) into V1,V2,V3,V4,V5 etc...
I have a regex that can get if there are three fields but the regex is not mapping if there are more or less than 3 fields.
I tried this regex
.?"FieldType":"(?<V1>\w+)".+?"FieldType":"(?<V2>\w+).+?"FieldType":"(?<V3>\w+)
Also I want to extract all the fields in fieldtype and ,EncryptedDocKey,Domain,Partner,Carrier,RequestTrackerId,Message,Formatters,ContentTypes,DeclaredType,StatusCode
It'd be unnecessary to apply regular expressions here, yet if you have to, an expression similar to:
"(FieldType)":"([^"]+)"|"(message)":"([^"]+)"
might extract those desired key values.
DEMO

Regex validator does not work with Django admin forms

I try to use a RegexValidator with a CharField, but I can't make it work...
class Configuration(models.Model):
name = models.CharField(verbose_name=u'Name', validators =
[RegexValidator(regex="[a-z]", message="Not cool", code="nomatch")])
I then just register it with
admin.site.register(Configuration)
But then in the admin forms, it accepts any possible name... Is the validation system suppose to work like that, or am I missing something ?
Your current regex checks that your value contains a single character from a-z. So it allows a, but it also allows a1.
Try changing the regex to:
regex=r"^[a-z]+$"
By including ^ and $ to mark the beginning and end of string, you make sure that your string contains only characters from a-z. The + allows multiple characters.

Match on ID in URL with RegEx

I am looking to find a random alpha-numeric ID from a URL returned in some JSON. Using the example:
"responseURL" : "http://sutureself.com/userid/123abc"
... I want to just match on the 123abc. So far I have:
http://sutureself.com/userid/([a-z0-9]+)
... but this matches the whole URL. What do I need to add to only match this ID? Note, the length of the ID can differ.
How about this:
[^/]+(?="})
Working regex example:
http://regex101.com/r/pT3fG8
Looks like you need to set the template to "$1". See comments in this related post.

Searching a number in a specific string with regexp in jmeter

I want to find a specific number from a HTML response.
For example, I want to extract 3 from publicationID3publicationID.
Does someone know a solution with regexp?
Add Regular Expression Extractor Post Processor as a child of the request, which returns to you this string.
Configure it as follows:
Reference Name: publicationID (you can use any variable name here)
Regular Expression: publicationID(\d+)publicationID
Template: $1$
other fields can be left blank.
You can later refer publication ID as ${publicationID} or ${__V(publicationID)}
You can see what matches does your Regular Expression return using View Results Tree Listener (select RegExp Tester from dropdown). Another option is Debug Sampler again with combination with View Results Tree.
you can use \d to match a number using regex.