Regex Class
(Automation::Regex)Header: | #include <Regex.h> |
Inherits: | CDPOperator<std::string> |
Public Functions
Reimplemented Public Functions
virtual bool | AddChild(const std::string &name, const std::string &typeName, const std::string &configuration) override |
virtual void | Configure(XMLPrimitive *operatorXML) override |
virtual void | Create(const char *pzName, CDPBaseObject *pParent) override |
virtual void | FillNodeChildren(CDP::StudioAPI::NodeStream &stream) const override |
virtual unsigned int | Process() override |
virtual bool | RemoveChild(const std::string &name) override |
- 18 public functions inherited from CDPOperator
- 17 public functions inherited from CDPOperatorBase
- 46 public functions inherited from CDPBaseObject
- 26 public functions inherited from CDP::StudioAPI::CDPNode
- 22 public functions inherited from CDP::StudioAPI::ICDPNode
Additional Inherited Members
- 1 public variable inherited from CDPOperatorBase
- 1 static public member inherited from CDPBaseObject
- 1 protected function inherited from CDP::StudioAPI::CDPNode
- 8 protected variables inherited from CDPOperator
- 5 protected variables inherited from CDPOperatorBase
- 11 protected variables inherited from CDPBaseObject
Detailed Description
The Regex operator searches substrings from input value using regular expression search pattern.
The Regex operator has the following properties:
Property | Description |
---|---|
Pattern | POSIX regular expression search pattern to use on In. |
Arguments
Argument | Description |
---|---|
In | The default input value. |
Out | The default output value. The operator bypasses the input value to output without change. |
Unlimited number of output arguments | Arguments to store the matched block (regex group) values into. |
The Regex search pattern is a string that describes the expected input, with special character entities that indicate blocks (groups) that will be copied to output argument values (in the order of occurrence).
Let's assume that an input string like this:
Temperature: 25.1 C; Humidity: 55 %
A Regex operator with the following Pattern can output values to temperature and humidity arguments:
Pattern="Temperature: (\S+) C;\s+Humidity: (\S+) %"
Regex search patterns can detect and copy values from very complex input strings.
Below is a table with the most useful special characters in patterns:
(...) | Specifies a block (regex group). Anything that is matched inside parenthesis is copied to value for the next argument in list. |
. | Matches any character |
\. | Matches . character (dot) |
\s | Matches any whitespace character (space, tab or newline) |
\S | Matches any non-whitespace character (any character except space, tab and newline) |
\d | Matches any digit character |
\D | Matches any non-digit character |
\w | Matches any alphanumeric character (letter, number or underscore) |
\W | Matches any non-letter character (so it is not a letter, number nor underscore) |
+ | Indicates one or more occurrences of the preceding elements. For example, S+ causes to match one or more sequential non-whitespace characters. |
* | Indicates zero or more occurrences of the preceding elements. For example, S* causes to match any number (or zero) sequential non-whitespace characters. |
You can learn more about regex possibilities, at:
Actual Processing Code of the Evaluate
unsigned int Regex::Process() { m_output = m_input; if (static_cast<string>(m_input) != m->cachedInput) { m->cachedInput = static_cast<string>(m_input); if (!m->cachedInput.empty() && m->regexOK) { boost::smatch matches; if (boost::regex_search(m->cachedInput, matches, m->regex)) { size_t index{1}; for (auto arg : m_arguments) { auto s = static_cast<std::string>(matches[index]); if (index < matches.size()) *arg = s; else arg->SetValue(""); // clear all non-matcing arguments ++index; } } else { GetParent()->ReportConfigurationFault(this, "Input '" + m->cachedInput + "' does not match the configured regex pattern '" + m->pattern + "'"); for (auto arg : m_arguments) arg->SetValue(""); // clear all arguments on pattern mismatch m->faultReported = true; return STATUS_SIGNAL_FAULT; } } else for (auto arg : m_arguments) arg->SetValue(""); if (m->regexOK && m->faultReported) { GetParent()->ClearConfigurationFault(this); m->faultReported = false; } } return STATUS_OK; }
See also Argument.
Member Function Documentation
Regex::Regex(const CDPPropertyBase &in)
Default constructs an instance of Regex.
[virtual]
Regex::~Regex()
Destroys the instance of Regex. The destructor is virtual.
[override virtual]
bool Regex::AddChild(const std::string &name, const std::string &typeName, const std::string &configuration)
[override virtual]
void Regex::Configure(XMLPrimitive *operatorXML)
[override virtual]
void Regex::Create(const char *pzName, CDPBaseObject *pParent)
[override virtual]
void Regex::FillNodeChildren(CDP::StudioAPI::NodeStream &stream) const
[override virtual]
unsigned int Regex::Process()
[override virtual]
bool Regex::RemoveChild(const std::string &name)
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.