regex

Regular expression common patterns

Import

_ <- fat.extra.regex

Constants

  • alphaOnly
  • alphaNum
  • digitsOnly
  • emailAddress
  • httpUrl
  • ipAddress
  • isbnCode
  • numericValue

Usage Notes

Here are some examples of how to match text against the regular expressions provided by this library using the match method of the Text prototype:

_ <- fat.extra.regex

"abc".match(alphaOnly)  # output: true

"abc123".match(alphaNum)  # output: true

"123".match(digitsOnly)  # output: true

"johndoe@example.com".match(emailAddress)  # output: true

"https://www.example.com/page?query=param".match(httpUrl)  # output: true

"192.168.0.1".match(ipAddress)  # output: true

"1-56619-909-3".match(isbnCode)  # output: true

"3.14159e-5".match(numericValue)  # output: true

Note that regular expressions may require modifications based on specific use cases or requirements. For example, you may need to modify the httpUrl regular expression to match URLs that include a port number. Be sure to test your own input data to make sure they are working as expected.

At the moment, FatScript's regex support is limited to matching only. You cannot use regular expressions for find and replace operations.

Technical details

Regular expressions can be very powerful tools, but they can also be complex and difficult to get right.

FatScript implements the POSIX regex extended dialect, which is the same dialect used by grep. Under the hood, FatScript uses the regexec function to perform regular expression matching.

Here is the exact implementation of regex provided by this library that may serve as inspiration to your own writing:

# alphaOnly: match one or more alphabet characters only
^[[:alpha:]]+$

# alphaNum: match one or more alphabet and digit characters
^[[:alnum:]]+$

# digitsOnly: match one or more digit characters only
^[[:digit:]]+$

# emailAddress: matches a valid email address, with one or more alphanumeric
# characters, dots, underscores, plus signs, or hyphens before the @ symbol,
# and one or more alphanumeric characters, dots, or hyphens after the @ symbol
# followed by a top-level domain of two to four letters
^[[:alnum:]_.+-]+@[[:alnum:]_.-]+\.[[:alpha:].]{2,4}$

# httpUrl: matches a valid http or https URL, the domain name (one or more
# alphanumeric characters followed by a dot), and the path (zero or more characters
# including alphanumeric characters, dots, hyphens, question marks, equal signs,
# ampersands, percent signs, or pound signs)
^(http|https):\/\/([[:alnum:]]+\.)+[[:alpha:]]{2,6}([\/[:alnum:]\.\-\?\=\&\%#]+)?$

# ipAddress: matches a valid IP address in dotted-quad notation, with four groups
# of one to three digits separated by periods
^([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$

# isbnCode: matches an ISBN that is either 10 or 13 digits long, with the last digit
# being either a digit from 0-9 or the letter "X" to represent 10, allowing for hyphens
# or spaces to be used as separators between groups of digits
^[0-9]{1,5}[- ]?[0-9]{1,7}[- ]?([0-9]{1,6}[- ]?[0-9]|[0-9][- ]?[0-9]{3,5})[- ]?[0-9X]$

# numericValue: matches a numeric value, including an optional negative sign at the
# beginning, one or more digits before an optional decimal part (a period followed by
# one or more digits), and an optional exponential part (letter 'e' followed by an
# optional sign and one or more digits)
^-?[[:digit:]]+(\.[[:digit:]]+)?(e[+-]?[[:digit:]]+)?$

When defining regular expressions in FatScript, prefer to use raw texts and remember to escape backslashes as needed, ensuring that the regular expressions are interpreted correctly.

See also

results matching ""

    No results matching ""