Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

To live is always desirable. -- Eleen the Capellan, "Friday's Child", stardate 3498.9


devel / comp.unix.shell / Re: Using expr for regexp match

SubjectAuthor
* Using expr for regexp matchJanis Papanagnou
+* Re: Using expr for regexp matchAxel Reichert
|`- Re: Using expr for regexp matchJanis Papanagnou
`- Re: Using expr for regexp matchChristian Weisgerber

1
Using expr for regexp match

<srumqr$tri$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4852&group=comp.unix.shell#4852

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Using expr for regexp match
Date: Sat, 15 Jan 2022 15:47:54 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <srumqr$tri$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jan 2022 14:47:55 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="44cffbc37c5457b1ad8c70212751ffe6";
logging-data="30578"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/IZrSsXQRPa7U9xmBKvGR4"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:yvK8n2tPdxzcU93REqC4ngyp0Vc=
X-Mozilla-News-Host: news://news.eternal-september.org
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 15 Jan 2022 14:47 UTC

I want to do some regexp matches on strings with expr, a tool I rarely
used, and I'm puzzled about its correct usage for the intended purpose,
if it's appropriate at all.

The test cases I am actually trying to implement are (in terms of grep)

$ grep -o "%[auUlLcCsS%]" <<< "A%aB%bC%cX"
%a
%c

$ grep -o "%[^auUlLcCsS%]" <<< "A%aB%bC%cX"
%b

or in terms of awk

$ echo "A%aB%bC%cX" |
awk '{ print match($0,/%[^auUlLcCsS%]/)
print substr($0,RSTART,RLENGTH) }'
5 %b

If possible I'd want either the substring or the index of the match in
the string and I thought that expr might serve as a light-weight tool
to avoid grep or awk.

The POSIX specs say: "Usually, the matching operator shall return a
string representing the number of characters matched ('0' on failure)."
so it might not be the appropriate tool. - Or am I missing something?

Janis

Re: Using expr for regexp match

<m2pmotymsm.fsf@axel-reichert.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4853&group=comp.unix.shell#4853

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: mai...@axel-reichert.de (Axel Reichert)
Newsgroups: comp.unix.shell
Subject: Re: Using expr for regexp match
Date: Sat, 15 Jan 2022 17:40:57 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <m2pmotymsm.fsf@axel-reichert.de>
References: <srumqr$tri$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dd3f5c23cae37fc6565801d363a76dc9";
logging-data="13983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O+N96fkEitTNgXHukUy8v4QtPj5p6+ew="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (darwin)
Cancel-Lock: sha1:Gez7D3hcgqhOvzLFqcm9xRPNMOQ=
sha1:1zUtICxmdOG6IgXlkrJjms+3ZPQ=
 by: Axel Reichert - Sat, 15 Jan 2022 16:40 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

> $ echo "A%aB%bC%cX" |
> awk '{ print match($0,/%[^auUlLcCsS%]/)
> print substr($0,RSTART,RLENGTH) }'
> 5
> %b

My (BSD) man page mentions

expr1 : expr2
The ``:'' operator matches expr1 against expr2, which must be a basic
regular expression. The regular expression is anchored to the begin-
ning of the string with an implicit ``^''.

If the match succeeds and the pattern contains at least one regular
expression subexpression ``\(...\)'', the string corresponding to
``\1'' is returned; otherwise the matching operator returns the num-
ber of characters matched. If the match fails and the pattern con-
tains a regular expression subexpression the null string is returned;
otherwise 0.

I got concerned by "achored", tested

expr "A%aB%bC%cX" : "%[^auUlLcCsS%]"

and got a "0" returned, which is not what you want.

expr "A%aB%bC%cX" : ".*\(%[^auUlLcCsS%]\)"

returns "%b", though. So your mileage may vary.

Best regards

Axel

Re: Using expr for regexp match

<sruv00$og7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4854&group=comp.unix.shell#4854

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Using expr for regexp match
Date: Sat, 15 Jan 2022 18:07:11 +0100
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <sruv00$og7$1@dont-email.me>
References: <srumqr$tri$1@dont-email.me> <m2pmotymsm.fsf@axel-reichert.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Jan 2022 17:07:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="44cffbc37c5457b1ad8c70212751ffe6";
logging-data="25095"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18rF3WilyHwaBHHAE2HHoUH"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:UGZKCHlGVg/r52SGjGds9ksXUPg=
In-Reply-To: <m2pmotymsm.fsf@axel-reichert.de>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 15 Jan 2022 17:07 UTC

On 15.01.2022 17:40, Axel Reichert wrote:
>
> I got concerned by "achored", tested
>
> expr "A%aB%bC%cX" : "%[^auUlLcCsS%]"
>
> and got a "0" returned, which is not what you want.

Yep, that was also my first test and the result puzzled me.

>
> expr "A%aB%bC%cX" : ".*\(%[^auUlLcCsS%]\)"

But this one is fine. I forgot about the subexpression semantics
and obviously was inattentive when re-reading the man page.

> returns "%b", though. So your mileage may vary.

Thanks!

Janis

Re: Using expr for regexp match

<slrnsu647k.2i3t.naddy@lorvorc.mips.inka.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4855&group=comp.unix.shell#4855

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Using expr for regexp match
Date: Sat, 15 Jan 2022 18:22:44 -0000 (UTC)
Message-ID: <slrnsu647k.2i3t.naddy@lorvorc.mips.inka.de>
References: <srumqr$tri$1@dont-email.me>
Injection-Date: Sat, 15 Jan 2022 18:22:44 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="84094"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 15 Jan 2022 18:22 UTC

On 2022-01-15, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

> I want to do some regexp matches on strings with expr, a tool I rarely
> used, and I'm puzzled about its correct usage for the intended purpose,

There are two properties to be aware of:
* expr(1)'s match operator uses a _basic_ regular expression.
* The regular expression is anchored to the beginning of the string
with an implicit '^'.

> If possible I'd want either the substring or the index of the match in
> the string and I thought that expr might serve as a light-weight tool
> to avoid grep or awk.

You can extract a substring with \(...\).

--
Christian "naddy" Weisgerber naddy@mips.inka.de

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor