[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What is wrong with a regex?
From: |
Dennis Williamson |
Subject: |
Re: What is wrong with a regex? |
Date: |
Fri, 3 Feb 2023 22:32:52 -0600 |
On Fri, Feb 3, 2023 at 10:12 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> On 2/3/23, Dennis Williamson <dennistwilliamson@gmail.com> wrote:
> > On Fri, Feb 3, 2023 at 9:59 PM Koichi Murase <myoga.murase@gmail.com>
> > wrote:
> >
> >> 2023年2月4日(土) 12:44 Peng Yu <pengyu.ut@gmail.com>:
> >> > $ f=row.txt; [[ $f =~ ^row([0-9]*)(\|_x)[.]txt$ ]]; echo $?
> >> > 1
> >>
> >> You need to write (|_x) instead of (\|_x). In the conditional command
> >> [[ ... ]], the character `|' loses the original meaning of the pipe
> >> operator, so you can directly specify it without quoting. If you quote
> >> it as \|, it becomes a regular expression that matches a literal
> >> single character `|'.
> >>
> >> $ [[ '|' =~ \| ]]; echo $?
> >> 0
> >>
> >>
> > Bash (and grep) don't allow an empty subexpression.
> >
> > f=foo; [[ $f =~ (|o) ]]; echo $?; echo "${BASH_REMATCH}"
> > 2
> >
> > $ echo foo | grep -E '(|o)'
> > grep: empty (sub)expression
>
> So then have to split it into two regexes? If I have two | with one
> branch empty, then I'd have 2x2=4 regexes. If I have n | with one
> branch empty, then I'd have 2^n regexes? If so, this becomes
> unmanageable. How to overcome this problem?
>
> --
> Regards,
> Peng
>
I don't know where that calculation comes from. This seems to work:
[[ $f =~ ^row([0-9]*)(_x\.txt|\.txt)$ ]]; echo $?
It's just one regex which specifies an optional sequence of digits and two
alternatives for the end of the string.
--
Visit serverfault.com to get your system administration questions answered.
- What is wrong with a regex?, Peng Yu, 2023/02/03
- Re: What is wrong with a regex?, Koichi Murase, 2023/02/03
- Re: What is wrong with a regex?, Dennis Williamson, 2023/02/03
- Re: What is wrong with a regex?, Leonid Isaev, 2023/02/04
- Re: What is wrong with a regex?, Koichi Murase, 2023/02/04
- Re: What is wrong with a regex?, Koichi Murase, 2023/02/04
- Re: What is wrong with a regex?, Jeffrey Walton, 2023/02/04
- Re: What is wrong with a regex?, Koichi Murase, 2023/02/04
- Re: What is wrong with a regex?, Leonid Isaev, 2023/02/04
Re: What is wrong with a regex?, Peng Yu, 2023/02/03