Score:0

Appending a pattern to a preexisting regex

jp flag

I have this regex that allows a space followed by visible characters

[[:space:]][[:graph:]]+$

How can I construct the possibilities of having either some pattern ptrn or the same pattern ptrn followed by the additional one above?

Max Ballard avatar
id flag
This is better suited for https://www.stackoverflow.com or https://unix.stackexchange.com
Score:1
hr flag

Awk uses POSIX extended regular expressions, so you can use ? as a quantifier to represent either zero or one occurrence of the preceding pattern:

$ printf '%s\n' 'ptrn' 'foo bar' 'ptrn bar' | awk '/ptrn([[:space:]][[:graph:]]+)?$/'
ptrn
ptrn bar

In GNU awk aka gawk you can also use a {n,m} quantifier:

$ printf '%s\n' 'ptrn' 'foo bar' 'ptrn bar' | gawk '/ptrn([[:space:]][[:graph:]]+){0,1}$/'
ptrn
ptrn bar
I sit in a Tesla and translated this thread with Ai:

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.