Skip to main content

Abnormal expressions (abnex) is an alternative to regular expressions (regex).

Project description

Abnormal expressions

Abnormal expressions (abnex) is an alternative to regular expressions (regex). This is a Python library but the abnex syntax could be ported to other languages.

Examples

Matching an email address

  • Regex

    • ([\w\._-]+)@([\w\.]+)
  • Abnex

    • {[w"._-"]1++}"@"{[w"."]1++}
  • Abnex (spaced)

    • {[w "._-"]1++} "@" {[w "."]1++}
  • Abnex (expanded)

    {
      [w "._-"]1++
    }
    "@"
    {
      [w "."]1++
    }
    

    A more advanced pattern:

    {{{[a-z '_']1++} {[a-z 0-9 '_-.']0++}} '@' {{[a-z 0-9]1++} '.' {[a-z 0-9]1++} {[a-z 0-9 '-_.']0++}} {[a-z 0-9]1++}}.

Why is Abnex Better?

  • It's easier to read, write and understand.
  • You can use spaces inside of the expression, you can also "expand" it, i.e. write it over multiple lines and use indention.
  • You don't have to use a backslashes all the time
  • More logical/common symbols like ! for not, {} for groups, 1++, 0++, 0+ for: one or more, zero or more, zero or one.
  • It's easier to see if a symbol is an actual symbol you are searching for or if it's a regex character, ex:
    • Regex: [\w-]+@[\w-_]+
    • Abnex: [w "-"]1++ "@" [w "-"]1++

Documentation

Regex on right after -> is the abnex equivalent

Anchors

  • Start of string, or start of line in multi-line pattern
    • ^ -> ->
  • End of string, or end of line in multi-line pattern
    • $ -> <-
  • Start of string
    • \A -> s>
  • End of string
    • \Z -> <s
  • Word boundary
    • \b -> :
  • Not word boundary
    • \B -> !:
  • Start of word
    • \< -> w>
  • End of word
    • \> -> <w

Character Classes

  • Control character
    • \c -> c
  • White space
    • \s -> _
  • Not white space
    • \S -> !_
  • Digit
    • \d -> d
  • Not digit
    • \D -> !d
  • Word
    • \w -> w
  • Not word
    • \W -> !w
  • Hexade­cimal digit
    • \x -> x
  • Octal digit
    • \o -> o

Quantifiers

  • 0 or more
    • * -> 0++
  • 1 or more
    • + -> 1++
  • 0 or 1
    • ? -> 0+

Groups and Ranges

  • Any character except new line (\n)
    • . -> *
  • a or b
    • a|b -> "a"|"b"
  • Group
    • (...) -> {...}
  • Passive (non-c­apt­uring) group
    • (?:...) -> {#...}
  • Range (a or b or c)
    • [abc] -> ['abc'] or ["a" "b" "c"]
  • Not in set
    • [^...] -> [!...]
  • Lower case letter from a to Z
    • [a-q] -> [a-z]
  • Upper case letter from A to Q
    • [A-Q] -> [A-Q]
  • Digit from 0 to 7
    • [0-7] -> [0-7]

Standards

What is the recommended way to write abnexes

  • Use spaces between characters in character sets:
    • Correct: [w "_-"]
    • Incorrect: [w"_-"]
  • Put multiple exact characters between the same quotes in character sets:
    • Correct: ["abc"]
    • Incorrect: ["a" "b" "c"], especially incorrect: ["a""b""c"]
  • Put spaces between groups:
    • Correct: {w} "." {w}
    • Incorrect: {w}"."{w}

Examples:

Match for an email address:

  • Regex:
    • [\w-\._]+@[\w-\.]+
  • Abnex (following standards):
    • {[w "-._"]1++} "@" {[w "-."]1++}
  • Abnex (not following standards):
    • {[w"-._"]1++}"@"{[w"-."]1++}

Functions (In Python)

Abnex has most functions from the re library, but it also has som extra functionality like: last() & contains().

Common functions between re and abnex

Regex on right after -> is the abnex equivalent

  • match() -> match()
  • findall() -> all()
  • split() -> split()
  • sub() -> replace()
  • subn() -> replace_count()
  • search() -> first()

Special to abnex

  • holds(): whether or not a string matches an expression (bool).
  • contains(): wheter or not a string contains a match (bool).
  • last(): the last match in a string.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

abnex-1.0.2.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

abnex-1.0.2-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file abnex-1.0.2.tar.gz.

File metadata

  • Download URL: abnex-1.0.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for abnex-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fa64afa5bf3f56a76c9ce82257b4fb682cf91c73975b2a0274c4be6c567669f0
MD5 1d476d4f0768448df34eca44e097ae4c
BLAKE2b-256 d24c7ae880b40146a03df4b13feb200f78d2374771457c46910d7b3428957aa0

See more details on using hashes here.

File details

Details for the file abnex-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: abnex-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for abnex-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c7ba7ef1335f76253a14fddc4a57d960769049a8b4675855b7e2b2f1c711ced
MD5 6dc403f9d7978891aa832bf5652a0fb1
BLAKE2b-256 40c8ad98a93b1c9409e6128a0f49e64e1644195529749df551e02e31b5c94e81

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page