Giter Site home page Giter Site logo

pregex's Introduction

Contributors MIT License

PRegEx - Programmable Regular Expressions

PRegEx is a Python module that can be used in order to construct Regular Expression patterns in a more human-friendly way.

Installation

You can start using PRegEx by installing it via pip:

pip install pregex

Usage

In PRegEx, everything is a Programmable Regular Expression, or "Pregex" for short. This makes it easy for simple Pregex instances to be combined into more complex ones! Within the code snippet below, we construct a Pregex instance that will match any URL that ends with either ".com" or ".org" as well as any IP address for which a 4-digit port number is specified. Furthermore, in the case that a match is a URL it's domain name will be separately captured as well.

from pregex.classes import AnyFrom, AnyDigit, AnyWhitespace
from pregex.quantifiers import Optional, Enforced
from pregex.groups import CapturingGroup
from pregex.tokens import Backslash
from pregex.operators import Either
from pregex.pre import Pregex

pre: Pregex = \
        Optional("http" + Optional('s') + "://") + \
        Optional("www.") + \
        Either(
            Enforced(AnyDigit() | AnyFrom(".")) +
            ":" + 
            4 * AnyDigit(),
            
            CapturingGroup(
                Enforced(~ (AnyWhitespace() | AnyFrom(":", Backslash())))
            ) +
            Either(".com", ".org")
        )

We can then easily fetch the resulting Pregex instance's underlying RegEx pattern.

regex = pre.get_pattern()

This is the RegEx pattern that the above method returns. Yikes!

(?:https?\:\/\/)?(?:www\.)?(?:[0-9\.]+\:[0-9]{4}|([^\r\:\n\x0c\\ \t\x0b]+)(?:\.com|\.org))

Besides from having access to its underlying pattern, we can use a Pregex instance to find matches within a string. Consider for example the following piece of text:

text = "text--192.168.1.1:8000--text--http://www.wikipedia.orghttps://youtube.com--text"

We can scan the above string for any possible matches by invoking the instance's "get_matches" method:

matches = pre.get_matches(text)

Looks like there were three matches:

['192.168.1.1:8000', 'http://www.wikipedia.org', 'https://youtube.com']

Likewise, we can invoke the instance's "get_groups" method to get any captured groups.

groups = pre.get_groups(text)

As expected, there were only two captured groups as the first match is not a URL and therefore there did not exist a domain name to capture.

[(None,), ('wikipedia',), ('youtube',)]

pregex's People

Contributors

manoss96 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.