You are not logged in.

#1 2010-09-03 12:03:01

LePeac
Member
From: Slovakia
Registered: 2009-04-18
Posts: 7

Meaning of regular expression

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

#2 2010-09-03 12:28:36

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Meaning of regular expression

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

#3 2010-09-03 13:46:00

LePeac
Member
From: Slovakia
Registered: 2009-04-18
Posts: 7

Re: Meaning of regular expression

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

#4 2010-09-03 13:47:38

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Meaning of regular expression

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'.

Offline

#5 2010-09-03 14:03:02

LePeac
Member
From: Slovakia
Registered: 2009-04-18
Posts: 7

Re: Meaning of regular expression

karol wrote:
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

#6 2010-09-03 14:04:06

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Meaning of regular expression

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 ?

By some clever algorithms... If you really want to know how it works, have a look at finite-state automata.

Offline

#7 2010-09-03 14:04:31

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Meaning of regular expression

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

#8 2010-09-03 14:41:32

LePeac
Member
From: Slovakia
Registered: 2009-04-18
Posts: 7

Re: Meaning of regular expression

I think I got it, but why when I change '*' to '+' there's no match ?

Offline

#9 2010-09-03 14:46:01

LePeac
Member
From: Slovakia
Registered: 2009-04-18
Posts: 7

Re: Meaning of regular expression

'+' must be escaped ..., it's clear to me now, thanks a lot ;-)

Offline

Board footer

Powered by FluxBB