(patch for Bash) match, strinterval
- From: William Park <opengeometry@xxxxxxxx>
- Date: Sat, 31 Mar 2007 07:14:56 +0000
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/
.
- Prev by Date: Re: Need help with sed to replace a URL
- Next by Date: Re: for of the Bash shell
- Previous by thread: (patch for Bash) 'readtrack', 'maskcard', 'creditcard'
- Next by thread: (patch for Bash) 'protobase', 'multi'
- Index(es):
Relevant Pages
|