Regex explanation..

"'Nick Anderson' via help-cfengine" <[email protected]> Thu, 21 Nov 2024 14:39:23 -0600
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
--=-=-=
Content-Type: multipart/alternative; boundary="==-=-="

--==-=-=
Content-Type: text/plain; charset="UTF-8"

  Your regex seems incorrect to me. You escaped the dot, but then you
  used `*' to repeat zero or more times. Try an extra dot in your regex,
  un-escaped?

  Better yet, don't use regular expressions to match on IP addresses if
  you can avoid it. Maybe `iprange()', or `isipinsubnet()' would be a
  good fit for you.

  - <https://docs.cfengine.com/docs/3.24/reference-functions-iprange.html>
  - <https://docs.cfengine.com/docs/3.24/reference-functions-isipinsubnet.html>

  Here I hacked at your example:

  ,----
  | bundle agent my_test
  | {
  |
  |   vars:
  |       "ipregex" string => "172\.16\.1\.*";
  |       "ipregex2" string => "172\.16\.1\..*";
  |
  |   classes:
  |       "found" expression => regcmp("$(ipregex)","172.16.1.10");
  |       "found2" expression => regcmp("$(ipregex2)","172.16.1.10");
  |
  |   reports:
  |     found::
  |       "Found the ip ";
  |
  |     found2::
  |       "Found the ip2 ";
  |
  |     !found::
  |       " Don't found the ip";
  |
  |     !found2::
  |       " Don't found the ip2";
  |
  |
  | }
  |
  | bundle agent __main__
  | {
  |       methods: "my_test";
  | }
  `----

  ,----
  | R: Found the ip2
  | R:  Don't found the ip
  `----

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/help-cfengine/878qtcnwxo.fsf%40northern.tech.

--==-=-=
Content-Type: text/html; charset="UTF-8"

<p>
Your regex seems incorrect to me. You escaped the dot, but then you used <code>*</code> to repeat zero or more times. Try an extra dot in your regex, un-escaped?
</p>

<p>
Better yet, don't use regular expressions to match on IP addresses if you can avoid it. Maybe <code>iprange()</code>, or <code>isipinsubnet()</code> would be a good fit for you.
</p>

<ul class="org-ul">
<li><a href="https://docs.cfengine.com/docs/3.24/reference-functions-iprange.html">https://docs.cfengine.com/docs/3.24/reference-functions-iprange.html</a></li>
<li><a href="https://docs.cfengine.com/docs/3.24/reference-functions-isipinsubnet.html">https://docs.cfengine.com/docs/3.24/reference-functions-isipinsubnet.html</a></li>
</ul>

<p>
Here I hacked at your example:
</p>

<div class="org-src-container">
<pre class="src src-cfengine3"><span style="color: #a89984; font-weight: bold;">bundle</span> <span style="color: #458588; font-weight: bold;">agent</span> <span style="color: #458588;">my_test</span>
<span style="color: #689d6a;">{</span>

  <span style="color: #a89984; font-weight: bold;">vars</span>:
      <span style="color: #689d6a;">"ipregex"</span> <span style="color: #d79921;">string</span> =&gt; <span style="color: #689d6a;">"172\.16\.1\.*"</span>;
      <span style="color: #689d6a;">"ipregex2"</span> <span style="color: #d79921;">string</span> =&gt; <span style="color: #689d6a;">"172\.16\.1\..*"</span>;

  <span style="color: #a89984; font-weight: bold;">classes</span>:
      <span style="color: #689d6a;">"found"</span> expression =&gt; regcmp<span style="color: #d79921;">(</span><span style="color: #689d6a;">"$(ipregex)"</span>,<span style="color: #689d6a;">"172.16.1.10"</span><span style="color: #d79921;">)</span>;
      <span style="color: #689d6a;">"found2"</span> expression =&gt; regcmp<span style="color: #d79921;">(</span><span style="color: #689d6a;">"$(ipregex2)"</span>,<span style="color: #689d6a;">"172.16.1.10"</span><span style="color: #d79921;">)</span>;

  <span style="color: #a89984; font-weight: bold;">reports</span>:
    <span style="color: #98971a; font-weight: bold;">found</span>::
      <span style="color: #689d6a;">"Found the ip "</span>;

    <span style="color: #98971a; font-weight: bold;">found2</span>::
      <span style="color: #689d6a;">"Found the ip2 "</span>;

    <span style="color: #98971a; font-weight: bold;">!found</span>::
      <span style="color: #689d6a;">" Don't found the ip"</span>;

    <span style="color: #98971a; font-weight: bold;">!found2</span>::
      <span style="color: #689d6a;">" Don't found the ip2"</span>;


<span style="color: #689d6a;">}</span>

<span style="color: #a89984; font-weight: bold;">bundle</span> <span style="color: #458588; font-weight: bold;">agent</span> <span style="color: #458588;">__main__</span>
<span style="color: #689d6a;">{</span>
      <span style="color: #a89984; font-weight: bold;">methods</span>: <span style="color: #689d6a;">"my_test"</span>;
<span style="color: #689d6a;">}</span>
</pre>
</div>

<pre class="example">
R: Found the ip2
R:  Don't found the ip
</pre>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &quot;help-cfengine&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:[email protected]">[email protected]</a>.<br />
To view this discussion visit <a href="https://groups.google.com/d/msgid/help-cfengine/878qtcnwxo.fsf%40northern.tech?utm_medium=email&utm_source=footer">https://groups.google.com/d/msgid/help-cfengine/878qtcnwxo.fsf%40northern.tech</a>.<br />

--==-=-=--

--=-=-=
Content-Type: text/plain; charset="UTF-8"




--
Nick Anderson | Doer of Things | (+1) 785-550-1767 | https://northern.tech

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/help-cfengine/878qtcnwxo.fsf%40northern.tech.

--=-=-=--