1

Why am I getting a syntax error?

% perl -ne 'if (/https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
syntax error at -e line 1, near "9A"
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.
Cœur
  • 37,241
  • 25
  • 195
  • 267
cnst
  • 25,870
  • 6
  • 90
  • 122
  • You weren't paying attention to my answer to your question on [`sed`, `awk`, `perl` or `lex` — find strings by prefix+regex ignoring rest of input](http://stackoverflow.com/questions/20233796/). It demonstrated the techniques shown in the answers here. – Jonathan Leffler Nov 27 '13 at 05:16
  • @JonathanLeffler, I did this independently (based on http://stackoverflow.com/a/20172099/1122270) and when your answer only had `sed` in it (note the missing `-l` here, too), but thanks for your help! – cnst Nov 27 '13 at 05:36

2 Answers2

5

If the regex contains slashes, use a different character and the explicit m operator:

perl -ne 'if (m%https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)%) { print $1 ; }'

Or:

perl -ne 'print $1 if m{https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)}'
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

You need backslashes in front of the // after https:

perl -ne 'if (/https:\/\/([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'

Otherwise it thinks the regex is already over.

woolstar
  • 5,063
  • 20
  • 31
  • duh, makes sense! \o\ |o| /o/ Is there any other way, to avoid the hand waving? ^_^ – cnst Nov 27 '13 at 05:13