You are not logged in.

#1 2015-12-22 13:19:43

mouseman
Member
From: Outta nowhere
Registered: 2014-04-04
Posts: 291

[solved] regular expression question

when grouping tokens together like this:

/\/web(\/.*)?/g

does that make the trailing '/' between the brackets () optional?

so it would match:
/web
/website
/web/1

or is it mandatory and match only:

/web/
/web/whatever

Figured out the above, but still can't get grep to work.

I've been trying to figure it out and test this expression with grep but it's not working. I created a simple test file:

web
/web
/web/
/web/1
web/2
/web/sdfk3

Doesn't seem to matter whether I escape or not, but it seems to trip over the grouping ().

So why doesn't either of the below work?

grep '\/web(\/.*)?' test
grep '/web(/.*)?' test
grep '/\/web(\/.*)?/g' test

Thanks!

Last edited by mouseman (2016-01-02 09:33:13)

Offline

#2 2015-12-22 14:18:35

oliver
Member
Registered: 2007-12-12
Posts: 448

Re: [solved] regular expression question

I hope I'm not horribly wrong here but grep is in basic regex mode by default (BRE) and you need extended (ERE)

Basic vs Extended Regular Expressions                                                                         
       In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special  meaning;  instead  use
       the backslashed versions \?, \+, \{, \|, \(, and \).

Try adding the -E flag to grep

Last edited by oliver (2015-12-22 14:19:06)

Offline

#3 2015-12-22 14:50:33

mouseman
Member
From: Outta nowhere
Registered: 2014-04-04
Posts: 291

Re: [solved] regular expression question

Ah, learned something about grep.

This works:

egrep '/web(/.*)?' test

Thanks!

Offline

Board footer

Powered by FluxBB