Code Explainers

Code explainers tagged #regex

python
import re
 
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator

Building a deconstructible validator in Django

validation regex deconstructible
Intermediate 7 steps
java
public List<Employee> parseEmployees(Path csvPath) throws IOException {
    List<Employee> employees = new ArrayList<>();
 
    try (BufferedReader reader = Files.newBufferedReader(csvPath, StandardCharsets.UTF_8)) {

Parsing a CSV into typed objects in Java

csv-parsing file-io try-with-resources
Intermediate 7 steps
python
import re
from collections import defaultdict
from pathlib import Path
 

Summarizing log files by date in Python

regex parsing aggregation
Intermediate 7 steps
javascript
function validateSignup({ email, password, confirmPassword, username, age }) {
  const errors = {};
 
  if (!email) {

Building a signup validator in JavaScript

validation regex guard-clauses
Beginner 7 steps
ruby
class EmailValidator < ActiveModel::EachValidator
  EMAIL_FORMAT = /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\z/i
 
  def validate_each(record, attribute, value)

Writing a custom email validator in Rails

validation regex custom validators
Intermediate 7 steps
python
import re
 
_UNIT_SECONDS = {
    "w": 604800,

Parsing duration strings like 1h30m in Python

regex parsing validation
Intermediate 8 steps
java
package com.example.logs;
 
import java.io.IOException;
import java.io.UncheckedIOException;

Counting HTTP statuses with Java streams

streams regex file-io
Intermediate 6 steps
java
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = StrongPasswordValidator.class)
@Documented

Building a custom @StrongPassword validator in Spring

bean-validation annotations regex
Intermediate 7 steps
php
<?php
 
namespace App\Support;
 

Building a URL slug from any title in PHP

regex transliteration string-processing
Intermediate 8 steps
ruby
class HostnameValidator < ActiveModel::EachValidator
  MAX_LENGTH = 253
  LABEL = /\A(?!-)[a-z0-9-]{1,63}(?<!-)\z/i
 

Writing a custom hostname validator in Rails

validation custom validator regex
Intermediate 8 steps
java
public final class RegistrationValidator {
 
    private static final Pattern EMAIL = Pattern.compile("^[\\w.+-]+@[\\w-]+\\.[\\w.-]+$");
    private static final Pattern USERNAME = Pattern.compile("^[a-zA-Z0-9_]{3,20}$");

Accumulating validation errors in Java

validation regex error-accumulation
Beginner 9 steps
python
import re
from datetime import datetime
from dataclasses import dataclass
 

Parsing access logs with named regex groups

regex named-groups dataclasses
Intermediate 6 steps
ruby
module TextAnalysis
  module_function
 
  WORD_PATTERN = /[\p{Alpha}']+/

Building a word-frequency analyzer in Ruby

text processing regex enumerable
Intermediate 8 steps
typescript
type SignupInput = {
  email: string;
  password: string;
  confirmPassword: string;

How a typed signup validator collects errors

form-validation type-safety regex
Intermediate 9 steps