|
The only regex(-like) syntax I felt somewhat comfortable working with was SNOBOL
That was 30+ years ago. I first met it as a 200 source lines version of Eliza, the therapist, which fascinated me immensely. Obviusly, that version never passed any Turing test, yet: Try to write anything comparable in 200 lines of any ordinary, algorithmic language! So I started playing around with it, just for fun - I never used it commercially.
Actually, not too long ago I picked up the source code of an old SNOBOL interpreter, hoping one day to port it. It is currently #43 on my project lists. Tuits are hard to find nowadays, especially round ones.
|
|
|
|
|
I like using regex for day-to-day, throwaway things. It's especially good for reformatting text. I'm certainly not intimidated by them.
That said, I don't think I would ever use one in product code with a long life-span. You must admit that regular expressions tend to be write-only, which is a cardinal sin against those who must maintain the code, including your future selves. Code written very concisely, and regular expressions may be the ultimate in concise, require a lot of mental unpacking during maintenance. Unless you write a ridiculous amount of comments for the expression, it might not be worth it.
Software Zen: delete this;
|
|
|
|
|
I think it depends. I generally agree that complicated regex is mug's game. However,
How do you technically, and accurately convey a set of rules around lexical requirements?
Such rules must be able to be conveyed to other developers precisely.
Such rules must be unambiguous, and testable.
Such rules must be absorbable in reasonable amount of time, meaning no poring over RFCs if one can avoid it.
Imagine conveying the rules for what constitutes a JSON number
You can either say:
(\-?)(0|[1-9][0-9]*)((\.[0-9]+)?([Ee][\+\-]?[0-9]+)?)
Which takes some unpacking as you say, but is certainly readable.
Or I can give you a page long document of requirements around JSON number parsing.
Personally, I can read that quite easily, but that's me.
Let me propose something - there is a meaningful subset of regular expressions which are easy to understand, and can fulfill most simple lexical specifications like the above, or say, like an email address, or an url, or any number of small, structured text fragments.
It beats the alternative, hands down.
Real programmers use butterflies
modified 3-Jan-21 11:31am.
|
|
|
|
|
To my mind that regex would be okay. It's the thousands of characters, wall-of-text abominations that I object to. I know, that's an example of poor use of regex, but it's the kind of thing you find. Inexperienced folks start using it, and all of a sudden it becomes their favorite toy.
A toy that's all sharp edges...
Software Zen: delete this;
|
|
|
|
|
I won't argue with that.
Real programmers use butterflies
|
|
|
|
|
I feel like if I were in charge of development practices at a given shop and regex were used as documentation as i suggested strict limits would be placed on its use
For starters [^-]().+*?| is all you get. That keeps it simple, portable, and non-backtracking. You can easily generate flow diagrams from it.
And it keeps people from getting ... "creative"
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: it keeps people from getting ... "creative" KISS: Keep It Stupid, Stupid.
Software Zen: delete this;
|
|
|
|
|
we use regex expressions in production code all the time. almost always validation.
phone numbers, emails, web addresses, and other pattern specialized validation.
never had any issues.
performance has never been an issue and accuracy has never been an issue. most of the uses require zero maintenance after implementation.
|
|
|
|
|
I like it.
Compared to indexOf, substr etc. it prevents me from dealing with IndexOutOfBound exceptions. I use it often for validation or parsing.
I also use it for configuration: Providing a single text field containing a regex, e.g. for selecting files to process, it gives a lot of flexibility at runtime (yes, it requires some knowledge by the administrator, but it can solve a lot of (unforeseen) requirements without changing the application).
However, you shouldn't solve *every* problem using regex.
|
|
|
|
|
I make what could be regarded as "heroic effort" to avoid using regex whenever possible. However, I used it in a recent application because it was the most expedient way to do what I needed.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Expedience is how it gets you. Next thing you know, you're hooked.
Real programmers use butterflies
|
|
|
|
|
I’ve resisted being hooked on its expedience since it’s inception. The way I see it, I will be able to easily make it to retirement (three years) without getting hooked on it.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I don't know that I'll live long enough to retire from coding, though i may retire from doing it professionally. I've always loved it. I suspect I always will.
Real programmers use butterflies
|
|
|
|
|
"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." - Jamie Zawinski
|
|
|
|
|
They're not that bad, Sander. Where's your sense of adventure?
Real programmers use butterflies
|
|
|
|
|
It can be quite intimidating at first. I found that Regex 101[^] is a great tool for learning to use regex, and I still use it to validate my regex for projects that I am working on.
Regards,
Thomas Stockwell
|
|
|
|
|
not afraid, but when you only need to make some regex once a year or less, It generally takes a bit to remember the all rules correctly to get the desired results. long regex's can be very cryptic, so I generally tend to break it down with lots of notes about each section for the future me that might have to modify it.
|
|
|
|
|
So i have a project called star social project this project is similar to a socail media that you can post and create group but this project you can only post when you are in a group. So i get an error message that is not familiar to me which is on the title, i tried to search on google and get some result but when i implement it to my project it does not work. So why im getting this error is because i'm trying to create a comment section and when i click the add comment that's when i get the error message. So i'm here to ask someone to help me because i'm not really familiar on this error and i'm just learning django for about 2 months now.
models.py
##########################
## POSTS MODELS.PY FILE ##
##########################
from django.contrib.auth import get_user_model
from django.db import models
from groups.models import Group
from misaka import html
from django.urls import reverse
from django.utils import timezone
User = get_user_model()
class Post(models.Model):
user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name='posts', null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.message
def save(self, *args, **kwargs):
self.message_html = html(self.message)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
'posts:single',
kwargs={
'username': self.user.username,
'pk': self.pk
}
)
class Meta:
ordering = ['-created_at']
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
username = models.CharField(max_length=50)
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=False)
def __str__(self):
return self.text
def approved(self):
self.approved = True
self.save()
views.py
class AddComment(LoginRequiredMixin, generic.CreateView):
model = Comment
fields = ('username', 'text')
def form_valid(self, form):
post = get_object_or_404(Post, pk=self.kwargs.get('pk'))
self.object = form.save(commit=False)
self.object.post = post
self.object.save()
return super().form_valid(form)
urls.py
########################
## POSTS URLS.PY FILE ##
########################
from django.urls import path
from posts import views
app_name = 'posts'
urlpatterns = [
path('', views.PostList.as_view(), name='all'),
path('by/<username>/', views.UserPost.as_view(), name='for_user'),
path('by/<username>/<int:pk>/', views.PostDetail.as_view(), name='single'),
path('new/', views.CreatePost.as_view(), name='create'),
path('delete/<int:pk>/', views.DeletePost.as_view(), name='delete'),
path('post/comment/', views.AddComment.as_view(), name='add_comment'),
]
comment_form.html
{% extends 'posts/post_base.html' %}
{% load bootstrap4 %}
{% block prepost %}
<h4>Add New Comment</h4>
<form id="commentForm" action="{% url 'posts:add_comment' %}" method="POST">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-large btn-primary">Add Comment</button>
{% endbuttons %}
</form>
{% endblock %}
_post.html
<div class="media">
<h3 class="mr-5"><a href="{% url 'posts:for_user' username=post.user.username %}">@{{ post.user.username }}</a></h3>
<div class="media-body">
{{ post.user.username }}
<h5>{{ post.message_html|safe }}
<time class="time"><a href="{% url 'posts:single' username=post.user.username pk=post.pk %}">{{ post.created_at }}</a></time>
{% if post.group %}
in <a href="#">{{ post.group.name }}</a>
{% endif %}
</h5>
<hr>
{% if user.is_authenticated %}
<a class="btn btn-primary btn-comment" href="{% url 'posts:add_comment' %}">Add Comment</a>
{% endif %}
<div class="container">
{% for comment in comment_list %}
<br>
{{ comment.created }}
<p>{{ comment.text|safe|linebreaks }}</p>
<p>Posted By: {{ comment.username }}</p>
{% endfor %}
</div>
<div class="media-footer">
{% if user.is_authenticated and post.user == user and not hide_delete %}
<a class="btn btn-simple" href="{% url 'posts:delete' pk=post.pk %}" title="delete">Delete</a>
{% endif %}
</div>
</div>
</div>
|
|
|
|
|
Read the red text in the upper-right corner of the page.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
Wrong place to post this, try here: Ask a Question[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
A for Asymptomatic
B for Blood Test
C for Corona
D for Dispensing
E for Epidemic
F for Flattening the Curve
G for "Go Corona Go"
H for Handwash
I for Incubation Period
J for Jobless
K for Kit (Immunity Kit)
L for Lockdown
M for Mask
N for Negative Report
O for Online
P for Pandemic
Q for Quarantine
R for Respirator
S for Sanitizer
T for Tracing
U for Unlock
V for Vaccine
W for Work From Home
X for ?
Y for Yellow Zone
Z for Zoom Call
(Again from WhatsApp)
modified 2-Jan-21 21:25pm.
|
|
|
|
|
Amarnath S wrote: X for ?
"X for "XXXXing virus!"
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
X for xenophobia - fear of the unknown, which we've all been experiencing to some degree or other
|
|
|
|
|
A certain site beginning with x and ending in videos for when you are bored perhaps?
|
|
|
|
|
X is for X-ray to look at the damage in yer lungs.
|
|
|
|
|