expansion in bash
There's this statement that I've come across in Advanced Bash Scripting (Inserting a blank line between paragraphs in a text file):
Code:
if [[ "$len" -lt "$MINLEN" && "$line" =~ [*{\.}]$ ]] |
It's somewhat confusing because many of those characters have special meaning in an extended regex, however, they're all used within a square brackets (list specification) so they're treated as literals (with the exception of the '.' which still has special meaning in a list and needs to be escaped).
So, unless I'm reading it wrongly, it will match any line that ends with any of '*' '{' '}' or '.' characters. edit: actually, I got that wrong. The \ and . are both literal too and it isn't an escape. God I hate reading regexs! ;) |
@GazL, IMHO a dot in a [character set] is a literal dot. No need for escaping it; the preceding backslash does nothing and can be omitted.
BTW a literal backslash in a [character set] DOES need another backslash. |
I don't know why I interpreted in such a complicated way. Now I understand that those are literal characters. I thought it was some special notation. Thank you :)
|
Quote:
e.g. Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes Oh, and don't you just love consistency: bash: Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes Code:
$ [[ 'wibble\' =~ [\.]$ ]] && echo yes |
Related to this are the following lines in the script:
Quote:
|
It's clearly some weird parsing bug in bash as regex='[\.]' ; [[ 'wibble\' =~ $regex ]] && echo yes works
|
Quote:
|
Small perplexment
Why isn't this a syntax error for an unmatched single quote?
Code:
test@ws1:~$ [[ 'wibble\' =~ [\.]$ ]] && echo yes |
I like how a seemingly simple question produced so much interesting stuff! :) I'm also interested in your question, josephj. What makes it more weird is that if you place the single quote in the regex between the square brackets, you do have to escape it and it only works that way.
|
Quote:
Things get a little more complicated within double-quotes and different shell implementations can vary with how they interpret the escape character: it will depend on what follows it. Basically it's all a bit of a mess. |
Regexes! Can't live with them. Can't live without them.
Regexes make me dizzy. I'm gradually getting better at them over the years, but I'm still a beginner.
I even read a whole O'Reilly book on them once and still didn't absorb much. I can't find the actual strip at the moment, but User Friendly summed it up nicely: Quote:
|
I don't get the joke, but I'm never any good at those :)
|
The bad old days ...
It doesn't work so well without the cartoon (he's looking over her shoulder at her CRT terminal) - and in the bad old days, we used 300 baud modems (and 110 before that - divide by roughly 10 for characters per second) to dial into time sharing systems. If the line quality was low, you'd get garbage characters sent or received (generated by noise on the transmission lines - the encoding used analog tones) - which don't look that different from a typical regex.
|
The realisation that there are generations out there that have never experienced serial terminals and line noise and need this joke explaining to them suddenly makes me feel very old.
|
All times are GMT -5. The time now is 04:35 AM. |