Re: Regex explanation..

Jerome Verleyen <[email protected]> Fri, 22 Nov 2024 09:19:30 -0600
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------0ryESnUoMayUVKRC6t5vVTZi
Content-Type: text/plain; charset="UTF-8"; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 21/11/2024 =C3=A0 14:39, 'Nick Anderson' via help-cfengine a =C3=A9crit=
=C2=A0:
>
> Your regex seems incorrect to me. You escaped the dot, but then you=20
> used |*| to repeat zero or more times. Try an extra dot in your regex,=20
> un-escaped?
>
> Better yet, don't use regular expressions to match on IP addresses if=20
> you can avoid it. Maybe |iprange()|, or |isipinsubnet()| would be a=20
> 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 =3D>"172\.16\.1\.*";
>        "ipregex2" string =3D>"172\.16\.1\..*";
>
>    classes:
>        "found" expression =3D> regcmp("$(ipregex)","172.16.1.10");
>        "found2" expression =3D> 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
> -

Dear Nick.

Thank's for your answer. I was wrong in this Regex... I forgot the=20
ultimate point caracter that is important in a anchored regex!

And i will use the iprange, that's help me a lot in other stuff with=20
cfengine..

Regards!



-- J=C3=A9r=C3=B4me
Pourquoi les =C3=A9tablissements ouverts 24 heures sur 24 ont-ils des serru=
res ?
	(Anonyme)

--=20
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 e=
mail to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/help-cfengi=
ne/a4473215-dfe0-4311-9a1e-f547e001e14c%40ibt.unam.mx.

--------------0ryESnUoMayUVKRC6t5vVTZi
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8=
">
  </head>
  <body>
    <div class=3D"moz-cite-prefix">Le 21/11/2024 =C3=A0 14:39, 'Nick Anders=
on'
      via help-cfengine a =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote type=3D"cite" cite=3D"mid:[email protected]">
      <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF=
-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=3D"org-ul">
        <li><a
href=3D"https://docs.cfengine.com/docs/3.24/reference-functions-iprange.htm=
l"
            moz-do-not-send=3D"true" class=3D"moz-txt-link-freetext">https:=
//docs.cfengine.com/docs/3.24/reference-functions-iprange.html</a></li>
        <li><a
href=3D"https://docs.cfengine.com/docs/3.24/reference-functions-isipinsubne=
t.html"
            moz-do-not-send=3D"true" class=3D"moz-txt-link-freetext">https:=
//docs.cfengine.com/docs/3.24/reference-functions-isipinsubnet.html</a></li=
>
      </ul>
      <p>
        Here I hacked at your example:
      </p>
      <div class=3D"org-src-container">
        <pre class=3D"src src-cfengine3"><span
        style=3D"color: #a89984; font-weight: bold;">bundle</span> <span
        style=3D"color: #458588; font-weight: bold;">agent</span> <span
        style=3D"color: #458588;">my_test</span>
<span style=3D"color: #689d6a;">{</span>

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

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

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

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

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

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


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

<span style=3D"color: #a89984; font-weight: bold;">bundle</span> <span
        style=3D"color: #458588; font-weight: bold;">agent</span> <span
        style=3D"color: #458588;">__main__</span>
<span style=3D"color: #689d6a;">{</span>
      <span style=3D"color: #a89984; font-weight: bold;">methods</span>: <s=
pan
        style=3D"color: #689d6a;">"my_test"</span>;
<span style=3D"color: #689d6a;">}</span>
</pre>
      </div>
      <pre class=3D"example">R: Found the ip2
R:  Don't found the ip
</pre>
      -</blockquote>
    <p>Dear Nick.</p>
    <p>Thank's for your answer. I was wrong in this Regex... I forgot
      the ultimate point caracter that is important in a anchored regex!</p=
>
    <p>And i will use the iprange, that's help me a lot in other stuff
      with cfengine.. <br>
    </p>
    <p>Regards!<br>
    </p>
    <p><br>
    </p>
    <p><br>
    </p>
    <pre class=3D"moz-signature" cols=3D"72">-- J=C3=A9r=C3=B4me=20
Pourquoi les =C3=A9tablissements ouverts 24 heures sur 24 ont-ils des serru=
res ?=20
	(Anonyme)
</pre>
  </body>
</html>

<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 e=
mail to <a href=3D"mailto:[email protected]">help-=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
help-cfengine/a4473215-dfe0-4311-9a1e-f547e001e14c%40ibt.unam.mx?utm_medium=
=3Demail&utm_source=3Dfooter">https://groups.google.com/d/msgid/help-cfengi=
ne/a4473215-dfe0-4311-9a1e-f547e001e14c%40ibt.unam.mx</a>.<br />

--------------0ryESnUoMayUVKRC6t5vVTZi--