Understanding regex behaviour when using quantifiers
So, in the above code , sed replaces at the start. does that mean sed using the pattern e* settles to zero occurence ? Why sed was not able to replace Teest string.
Some flavors of regex have + for one or more, but you can just say 'ee*'. Also, there is '\{1,99\}' for 1 to 99 in the sed flavor. There must be about a dozen regex flavors, especially after the PERL guys dominated a POSIX version, so the word edge '\>' became '\b': Regex Tutorial - \b Word Boundaries
There are even schemes to make the * lazy as opposed to the normal greedy behavior. Consider the ksh/bash ${pathname##*/} is greedy, leaves just the entry name, but the ${pathname#*/} just removes the first slash and anything before it. This is not a standard regex, but I recall MULTICS qedx having a way to do the agressive/lazy switch back when. I wonder if regex are older than UNIX?
The g says how many times to apply the substitution: infinite. You can also say 3 to skip to the third match before substituting. It has to do with the writing, not the matching. With no flag, same as 1. http://www.regular-expressions.info/possessive.html
So, in the above code , sed replaces at the start. does that mean sed using the pattern e* settles to zero occurence ? Why sed was not able to replace Teest string.
How does it work when global flag turned on ?
Despite what DGPickett said, perl had no affect on the description of regular expression in the POSIX standards.
There are several variations on RE processing, but there are three main types (basic regular expressions [BREs], extended regular expressions [EREs], and pathname pattern matching) in the standards (POSIX, the Single UNIX Specification [SUS], the System V Interface Definition [SVID], and the Linux Standard Base [LSB]). According to POSIX, SUS and SVID, sed and a bunch of other utilities use BREs, awk and a bunch of other utilities use EREs, and the shell and a bunch of other utilities use pathname pattern matching when expanding pathnames. POSIX has about five full pages describing BREs, three and a half pages that describe the differences between BREs and EREs and another four and a half pages that give the formal grammar for the interpretation of BREs and EREs, and about two and half pages that describe the differences between pattern matching and REs.
Some utilities (like grep) have options to choose between BREs, EREs, and fixed strings. Although not specified by the standards, some implementations have options for sed to choose between BREs and EREs, but using EREs with sed is not portable.
Meanwhile, back to your question. In the BREs used in sed, the expression e* matches zero or more occurrences of an e. The beginning of the string Teest string (before the "T") matches zero occurrences of "e", so the pipeline:
produces:
and the command:
would replace every occurrence of zero or more "e"s with your replacement string. I.e.,
Two portable sed pipelines to do what you were trying to do are:
or (as DGPickett suggested):
which replaces the first occurrence of one or more "e"s with the specified replacement string. In this case:
These 2 Users Gave Thanks to Don Cragun For This Post:
FWIW: The standards issues with regex are something that that appears to be coming together well. Or better anyway.
Basically when you are using UNIX tools, IMO, regex use has this sort of feel to it:
This is the way UNIX was overall back in the 90's - XOPEN, SUS, SVID, SYSV, BSD, Torvalds etc.
Henry Spencer ( zoologist) wrote the first open source version of UNIX regex, which then allowed the creation of cascade of modern regex "flavors". Larry Wall appears to have used Spencer's regex as a model for perl regex, for example.
So, if you understand the difference between extended regular expressions (ERE) and basic (BRE) you are well on the way.... to Belgium.
Hello All,
While googling on regex I came across a site named Regulex Regulex:JavaScript Regular Expression Visualizer
I have written a simple regex ^(a|b|c)(*)@(.*) and could see its visualization; one could export it too, following is the screen shot.
... (3 Replies)
Hi,
I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works:
LOCAL_CONFIG
#
Kcheckaddress regex -a@MATCH
+<@+?\.++?\.(us|info|to|br|bid|cn|ru)
LOCAL_RULESETS
SLocal_check_mail
# check address against various regex... (0 Replies)
Hi everyone,
This regex looks simple and yet it doesn't make sense how it's manipulating the output.
ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0c:49:c2:35:6v
inet addr:192.16.1.1 Bcast:192.168.226.255 Mask:255.255.255.0
inet6 addr:... (2 Replies)
I am not a big expert in regex and have just little understanding of that language.
Could you help me to understand the regular Perl expression:
^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{
------
This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Hi Guys,
Could you please kindly explain what exactly the below SED command will do ?
I am quite confused and i assumed that,
sed 's/*$/ /'
1. It will remove tab and extra spaces .. with single space.
The issue is if it is removing tab then it should be Î right ..
please assist.... (3 Replies)
I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here?
find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Hi,
Please help me to understand the bold segments in the below regex.
Both are of same type whose meaning I am looking for.
find . \( -iregex './\{6,10\}./src' \) -type d -maxdepth 2
Output:
./20111210.0/src
In continuation to above:
sed -e 's|./\(*.\{1,3\}\).*|\1|g'
Output: ... (4 Replies)
I have these two files in current dir:
oos.txt
oos_(copy).txt
I execute this find command:find . -regex './oos*.txt'And this outputs only the first file (oos.txt)! :confused:
Only if I add another asterisk to the find find . -regex './oos*.*txt' do I also get the second file... (7 Replies)
I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly:
if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)