You are not logged in.
Pages: 1
Hello, I'm just learning bash and I'm a little confused about meaning of this regular expressions.
13:28 -rohac test >> ls -l | grep ^- |grep '\<[^ ]*[[:digit:]][^ ]*$'
-rw-r--r-- 1 rohac users 0 sep 2 23:03 test1
-rw-r--r-- 1 rohac users 0 sep 2 23:03 test11
-rw-r--r-- 1 rohac users 0 sep 2 23:03 te1st
-rw-r--r-- 1 rohac users 0 sep 2 23:03 te11st
-rw-r--r-- 1 rohac users 0 sep 2 23:03 1test
-rw-r--r-- 1 rohac users 0 sep 2 23:03 1test1
-rw-r--r-- 1 rohac users 0 sep 2 23:03 11test
if '\<[^ ]*' matches any nowhite character at the begining of the word zero or more times, why it also show file "1test" ? what match '[[:digit:]]', if '\<[^ ]*' match '1' ? I'm sure it's pretty simple and that I miss something, but can somebody explain it to me ?
Offline
You said it yourself, the Kleene star denotes _zero_ or more times the subregex it applies to. So the string 1test matches if you take zero times the [^ ] then the [[:digit:]] for the 1 and then four times the second [^ ] for the rest of the word.
Offline
but how bash know, how to evaluate regexp ? how it know that '[^ ]*' should be matched exactly zero times, ando no one or more times ?
Offline
but how bash know, how to evaluate regexp ? how it know that '[^ ]*' should be matched exactly zero times, ando no one or more times ?
IIRC '*' means 'zero or more' and '+' means 'one or more'.
Offline
LePeac wrote:but how bash know, how to evaluate regexp ? how it know that '[^ ]*' should be matched exactly zero times, ando no one or more times ?
IIRC '*' means 'zero or more' and '+' means 'one or more'.
Yes, I know that, but why in this case it match zero times, and no more ? Is there any order in regexp's parts evaluation, say, '[[:digit:]]' evaluates first, then '[^ ]*' etc ?
Sorry if I ask dumb questions, but i need to know such things to understand it
Offline
but how bash know, how to evaluate regexp ? how it know that '[^ ]*' should be matched exactly zero times, ando no one or more times ?
By some clever algorithms... If you really want to know how it works, have a look at finite-state automata.
Offline
IIRC it's called laziness - they stop at the first match.
The algorithms can also be greedy, meaning they pick the longest pattern. If you search for 'a*a' in a line 'ababababa' it will match the whole line.
Last edited by karol (2010-09-03 14:09:18)
Offline
I think I got it, but why when I change '*' to '+' there's no match ?
Offline
'+' must be escaped ..., it's clear to me now, thanks a lot ;-)
Offline
Pages: 1