Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

net.almson:almson-regex

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

net.almson:almson-regex

almson-regex is a simple library for writing _readable_ regular expressions.

  • 1.5.1
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

Introduction

almson-regex is a simple library for writing readable regular expressions.

The goals of this library are:

  • Descriptive syntax
  • Good documentation
  • Support of all Java regex features

The following are not goals:

  • Type safety (keep it simple, use Strings!)
  • Extreme brevity (if you want super-compact, illegible regular expressions, write them the old way!)
  • Allow user to avoid learning regular expressions (fundamentally, you're still writing and reading regexes, but the descriptive names and good documentation makes it much easier!)

almson-regex is based on string operations, and is easy to use for all, some, or parts of your regular expressions. almson-regex is compiled with Java 8 and supports all Java 17 regex features, such as named capturing groups and glyph cluster matchers.

The documentation for the library doesn't replace knowledge of how regular expressions work. However, this library succeeds in making your regular expressions easy to read by those who do not have expert knowledge. For the best reference on Java regular expressions, see the java.util.regex.Pattern documentation.

Installation with Maven

<dependency>
    <groupId>net.almson</groupId>
    <artifactId>almson-regex</artifactId>
    <version>1.5.1</version>
</dependency>

Examples

Match leading or trailiing whitespace:

String regex = "^[ \t]+|[ \t]+$"

becomes:

String regex = 
    either(sequence(START_BOUNDARY, oneOrMore(HORIZONTAL_WHITESPACE)),
           sequence(oneOrMore(HORIZONTAL_WHITESPACE), END_BOUNDARY))

but since sequence simply does string concatenation, we can also write:

either(START_BOUNDARY + oneOrMore(HORIZONTAL_WHITESPACE),
       oneOrMore(HORIZONTAL_WHITESPACE) + END_BOUNDARY)

Match an IP address (simple version):

"\\b(\\d{1,3}\\.){3}\\d{1,3}\\b"

becomes:

WORD_BOUNDARY +
exactly(3, between(1, 3, DIGIT) + text(".")) +
between(1, 3, DIGIT) +
WORD_BOUNDARY

Match an email address (simple version):

"\\b(<user>[a-zA-Z0-9._%+-]+)@(?<domain>[A-Z0-9.-]+\.\\p{L}{2,})\\b"

becomes:

WORD_BOUNDARY + 
namedGroup("user", 
           oneOrMore(charclassUnion(LETTER, DIGIT, charclass('.', '_', '%', '+', '-')))) + 
text("@") + 
namedGroup("domain", 
           oneOrMore(charclassUnion(LETTER, DIGIT, charclass('.', '-'))) + 
text(".") + 
atLeast(2, LETTER)) + 
WORD_BOUNDARY

Select consecutive duplicates from a comma-delimited list

"(?<=,|^)([^,]*)(,\1)+(?=,|$)"

becomes:

precededBy (either (START_BOUNDARY, text (","))) + 
group (zeroOrMore(charclassComplement(charclass(',')))) + 
oneOrMore (text(",") + backreference(1)) + 
followedBy (either (text(","), END_BOUNDARY))

FAQs

Package last updated on 13 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc