(patch for Bash) match, strinterval



These 2 builtin commands can do regex or fixed-string matching. They
can match for single pattern, or for interval with "begin" and "end"
pattern.

match [-s] string regex [submatch]
Return success (0) if STRING contains REGEX pattern. If SUBMATCH is
specified, then it will contain prefix before regex match, entire matching
string (REGEX), any parenthesized groups in regex pattern (group[1],
group[2], ...), and suffix after regex match. That is,
submatch=( prefix REGEX group[1] group[2] ... group[n-1] suffix )

If shell option 'nocaseglob' is set, then match is case-insensitive. With
-s option, the pattern is treated as fixed string instead of regex, in
which case SUBMATCH will contain 3 elements, ie.
submatch=( prefix REGEX suffix )

strinterval [-r] string begin end [submatch]
Extract substring, delimited by non-overlapping BEGIN and END patterns. By
default, the patterns are simple string, but regex(7) pattern can be used
with -r option. Returns success (0) if patterns are found, or failure (1)
if patterns are not found. When patterns are found, there are 5 segments
to consider:
string --> submatch=( prefix BEGIN middle END suffix )
All 5 segments are returned in array variable SUBMATCH (if given) which is
always flushed first.

These 2 builtin commands were posted before, but API has changed.
Before, only regex and subgroups were returned in SUBMATCH. Now, the
leading prefix and trailing suffix are returned as well.

For example,

a=abc123xyz
match $a '[0-9]+' submatch --> submatch=( abc 123 xyz )
strinterval $a 'c1' '3x' submatch --> submatch=( ab c1 2 3x yz )

Ref:
http://home.eol.ca/~parkw/index.html#match
http://home.eol.ca/~parkw/index.html#strinterval

--
William Park <opengeometry@xxxxxxxx>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
.



Relevant Pages

  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex
    ... case sensitivity was part of the problem which I fixed with ... In other word I got syntax problem with the month pattern ... >>against the articles and help on regex, I still can't find the mistake I ... >> Public Function regtest(ByVal StringIn As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I shorten or split this function
    ...    (scan regex string) ... Instead of field-name you mean field-type. ... it doesn't test the string to see if it's of the suitable form for the ... corresponding field of the pattern. ...
    (comp.lang.lisp)
  • Re: Expressing AND, OR, and NOT in a Single Pattern
    ... much better because the esoterica of regex can make the desired ... results hard to figure out and the bugs in the pattern even harder to ... The only thing I'd do differently to these patterns is add an anchor ... long strings, when the pattern was at the end of the string. ...
    (comp.lang.perl.misc)
  • Re: Interaction between two strings
    ... then the string XYZC should give a match. ... > string that is supplied separately from the regex. ... $patterns, you splice them all into one very big $pattern. ... Doing this is immensely faster than iterating through an array or the like: ...
    (comp.lang.perl.misc)