Itemized Meaning of the RegEx: "(/.*)?"

I am trying to determine each element of this regular expression: "(/.*)?"

Here is what I have learned on my own so far:

() == grouping regex together

/ == ???

. == equal to any one character

* == equal to zero or more of the preceding character (in this case, would the previous character be "."?)

? == equal to zero or one of the preceding character (probably anything/everything represented by (/.*) right?)

Can I get help in filling the gaps? For example: What does "/" mean in this context?

1 Answer

First of all, there are several different flavours of regular expressions; simple grep, extended egrep, Perl-like; and probably more. They are subtly different; it starts with grouping like (foo|bar|baz) which pure grep does not support, but egrep and Perl do.

In your concrete example, you deciphered the parts correctly. / is literally a forward slash without any special meaning. If it were a backslash \, it would escape the following character, i.e. if it has a special meaning in that context, it would lose that special meaning: \. means "a literal period", not "any character".

And here is the catch with the different variants: You need to know which one is relevant in your context. If it's extended regular expressions (egrep or Perl), ( and ) really means grouping, if it's simple regular expressions (grep), it's literal parentheses.

Whole books have been written on the topic, and there are lots of tutorials on the web. You might want to start with man grep and then work you way forward.

Many exotic parts of regexps are very rarely needed; you can mostly get by with the basics and look up more arcane things when you need them (or when you have to make sense of somebody else's code).

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like