Skip to content

Perform string matching, extraction or substitution using a regex pattern

License

Notifications You must be signed in to change notification settings

vdelachaux/Regex-with-Classes

Repository files navigation

Static Badge  Static Badge
release license

Regex-with-Classes

The goal of this class is to reduce the complexity of code to use Regex in 4D.

Code sample

var $regex : cs.regex
$regex:=cs.regex.new("Hello world, the world is wonderful but the world is in danger"; "Hello (world)")

var $match : Boolean
$match:=$regex.match()  // = True

var $c : Collection
$c:=$regex.extract()  // = [ "Hello world", "world" ]
$c:=$regex.extract(1)  // = [ "world"]

var $result : Text
$result:=$regex.substitute("Vincent")  // = "Vincent, the world is wonderful but the world is in danger"

Documentation

The complete class documentation is available here and is also displayed in the Explorer documentation panel.

The test_regex method will help you learn how to use it.

Installation

Static Badge Static Badge

This repository is compatible with the Project dependencies feature. So you can simply integrate this component into your project by selecting Design > Project dependencies and adding vdelachaux/regex-with-classes as the repository address in the dedicated dialog box. This way, you can benefit from updates over time.

The published namespace is "rgx", so the class instantiation in your project will therefore be:

$regex:=cs.rgx.regex.new()

Copy of the class

Copy into your database folder:

  • The regex.4dm file from this repository to the ~/Project/Sources/Classes/ folder
  • The regex.md file from this repository to the ~/Documentation/Classes/ folder

The class instantiation will therefore be:

$regex:=cs.regex.new()

⚠️ In this case, you'll need to check the repository regularly for updates, subscribe to it to receive update notifications, or clone it. You'll then need to transfer the changes to your project.

Contributing

This class will be augmented according to my needs but I strongly encouraged you to enrich this project through pull request. This can only benefit the 4D developer community

📌 This code is an evolution of the regex component.

References

4D documentation

Regex documentation

Tools

  • RegexLab component
  • macOS RegExRX. An application I use regularly to test my Regex. It allows you to copy them, once finalized in a format compatible with 4D's code editor, and even to copy the FR or US code like this:
ARRAY LONGINT($rxPositions;0)
ARRAY LONGINT($rxLengths;0)
$rxPattern:="(?mi-s)world"
$rxMatch:=Match regex($rxPattern;$sourceText;1;$rxPositions;$rxLengths)

 

Enjoy the 4th dimension