Urgent.News

What's breaking now, across thousands of outlets.

Tech

Regex Cheatsheet for Beginners (With Copy-Paste Examples)

Regular expressions look cryptic at first, but a small set of building blocks covers most real-world tasks: validating input, searching logs, and doing bulk find-and-replace. Learn these pieces and you can read and write most patterns you'll meet. The basic building blocks . matches any single character except a newline. \d matches a digit, \w a word character (letters, digits, underscore), and…

Regular expressions can seem intimidating at first, but understanding a few key components allows you to tackle most real-world tasks. Some basic building blocks include the dot character ., which matches any character except a newline; \d for digits; \w for letters, digits, and underscores; and \s for whitespace. The capital versions \D, \W, and \S match the opposite.

Character classes let you match a group of options. For example, [abc] matches any single a, b, or c; [a-z] matches any letter; and [^abc] matches anything except a, b, or c. The ^ and $ characters match the beginning and end of a string, or the end of each line when using the m flag. The \b metacharacter stands for a word boundary—a handy check for whole-word matches.

To specify how many times a character or group appears, you can use quantifiers. An asterisk * means zero or more occurrences, a plus + means one or more, and a question mark ? means zero or one. Braces {3} specify exactly three occurrences, {2,5} between two and five, and {2,} two or more. Quantifiers are "greedy" by default, meaning they grab as many characters as they can. Adding a ? after one—like .*?—makes it "lazy," causing it to match as few characters as possible.

You can group parts of a pattern together with parentheses to create subpatterns, alternate between alternatives using the | symbol, and create backreferences to reuse parts of a match. For example, (cat|dog)s? will match cat, cats, dog, or dogs. Named capturing groups, like (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}), give meaningful names to parts of a pattern, which can be useful when extracting specific pieces later.

Lookahead and lookbehind assertions check what surrounds a position without including it in the match. Positive lookahead \d+(?= dollars) matches digits followed by the word "dollars," while positive lookbehind (?<! dollars)\d+ matches digits not followed by "dollars." The syntax for lookarounds is slightly different in various regex flavors.

Flags like g enable finding all matches rather than stopping at the first one; i makes the match case-insensitive; m causes ^ and $ to match at line boundaries; and s lets . also match newline characters. Common regex patterns include validating ISO dates, hexadecimal colors, and email addresses. However, regex alone cannot verify the actual existence of an email address or date. Always remember that regex validates shape, not truth.

When using regex, avoid common pitfalls such as forgetting to escape special characters, using greedy quantifiers when you need lazy ones, nesting quantifiers that lead to catastrophic backtracking, and assuming all engines behave identically. Start by testing your patterns against sample text, adding pieces one at a time, and observing the results. Many web-based regex testers allow you to experiment without uploading any files.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Base64 Is Not Encryption (And Other Common Misconceptions)

Base64 shows up everywhere: emails, data URLs, HTTP headers, JWTs. Because the output looks scrambled, it is often mistaken for encryption. It isn't.

  • Base64 is an encoding method, not encryption
  • It converts binary data to text format, reversible by anyone
  • Used for text-only channels, not providing secrecy

How Strong Should a Password Be? Length vs. Complexity

Password strength is really a question of how many guesses an attacker would need. Every extra character multiplies that number, which is why length matters far more than swapping an a for an @.

  • Length of password exponentially increases possible guesses for attacker
  • Entropy measures password unpredictability in bits, doubles with each bit
  • Passphrases made of random words offer strong security and memorability

How to Check Color Contrast for Accessibility (WCAG Explained)

Low-contrast text is one of the most common accessibility failures on the web, and one of the easiest to fix. It affects people with low vision and color-vision differences, but also anyone reading a…

  • WCAG quantifies contrast via ratio measuring luminance between colors.
  • Level AA requires minimum 4.5:1 contrast ratio for normal text.
  • Non-text elements like input borders need a minimum 3:1 contrast.

JSON vs YAML: When to Use Which

JSON and YAML can describe the same data, so the choice comes down to who (or what) reads and writes the file. JSON is strict, small, and easy for software to parse.

  • JSON is strict and compact, ideal for software parsing
  • YAML is forgiving and user-friendly for humans
  • Use JSON for software-to-software data exchange

Why You Should Remove EXIF Data Before Sharing Photos

Every photo from a phone or camera carries a block of hidden information called EXIF (Exchangeable Image File Format) metadata.

  • Removing EXIF data before sharing photos prevents revealing personal information.
  • EXIF metadata includes device specifics, timestamps, and location data.
  • Use browser-based tools to strip metadata without uploading photos.

More from Saturday 26 September →