I'm trying to match all strings with a known prefix and a mostly known suffix.
The prefix will be any 3 uppercase characters.
The suffix will be one uppercase C
and zero or one numbers afterward. ex. C
or Cx
where x
is any number
The middle substring is of unknown length and is uppercase letters only.
GORABJKAC3 [match]
GORCCCCC [match]
GORBBBBCCC [match
GORBBBBCA [no match]
BORBBBBCA2 [no match]
I tried something like grep ^GOR[:upper:]*C[:digit:]*
but that doesn't work.
I think [:upper:]
may just consume all uppercase letters, including the suffix C
I want to match.
How can I match my desired string with regex using grep?
You can use this regex:
\b[A-Z]{3}.*?C[0-9]?\b
Or using anchor (if these strings are on separate lines):
^[A-Z]{3}.*?C[0-9]?$
Use [AZ]
instead of [:upper:]
and [0-9]
instead of [:digit:]
.
Also, *
means 0 or more, +
means 1 or more, and ?
means 0 or 1. I think you want to be using +
and ?
.
+
and ?
are special Perl regex characters so add the -P
flag to your grep command.
So final regex : grep -P ^GOR[AZ]+C[0-9]?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.