Score:0

Testing Regular Expressions

eg flag

I am trying to learn regular expressions, and came across some examples online. Trying to put things together, I inputted this into bash

^(([a-j][a-j]?)|(3[a-j][a-j])$

It returns the following error

bash: !!: event not found.

Why do you believe I am getting that? Should I create a to j files? or should I create 1 file with a-j in it? Why is it returning that? Thank you for your help.

fo flag
Please show exactly how you used that. Cut-and-paste if possible.
Smokefox avatar
eg flag
Hi Glen. I was just testing it as I input it above. e.g. I tested ^/([0-9])|([0-9])$ and I get a different error "substitution failed". So I am just trying to understand the core of it. I have read various articles about expressions but need help in understanding.
fo flag
So you're just typing that into a bash prompt? What do you expect to happen? In bash, you can only use a regular expression as part of a `[[ $var =~ regex ]]` conditional command. See [3.2.5.2 Conditional Constructs](https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs)
ph flag
Shell syntax is extremely context-dependent; bash will only treat something as a regular expression if it occurs in a place where bash expects to see a regular expression (i.e. in `[[ $var =~ regex ]]`). Since you used that in a general command context, bash saw the `^` and interpreted it as a command history substitution (i.e. it thinks you're telling it to recall and re-use some part of a previous command), because that's what `^` means in a general command context. But it couldn't find a matching previous command ("event"), so you get a "event not found" error.
Smokefox avatar
eg flag
@glennjackman thank you that is the answer I needed.
Smokefox avatar
eg flag
@GordonDavisson Thank you for the breakdown of the matter. I understand it better now.
us flag
You can use https://regexr.com to play with regular expressions.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.