LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2022, 12:08 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
matching colour escape codes


I am matching colour escape codes e.g. \033[21;41;32m.

How can I improve on the following

Code:
if [[ "$1" =~ \[[0123456789]+\;[0123456789]+\;[0123456789]+m ]]
 
Old 05-02-2022, 08:34 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Slightly shorter, but otherwise the same:

Code:
if [[ $1 =~ \[[0-9]+\;[0-9]+\;[0-9]+m ]]
 
Old 05-02-2022, 02:39 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Could one also group the first two [0-9]+\; in a single expression?
 
Old 05-02-2022, 03:38 PM   #4
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Are colour formats like

Code:
\033[21;41;32;m
acceptable rather than the usual

Code:
\033[21;41;32m
 
Old 05-02-2022, 11:34 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you got a very good link explaining it: https://misc.flogisoft.com/bash/tip_...and_formatting
why don't you read it?
And why don't you try it?
 
Old 05-03-2022, 04:49 PM   #6
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Figured that it might be interpreted as \033[21;41;32;0m.

This means that I cannot do

Code:
if [[ $1 =~ \[ ([0-9]+\;){3}m ]]
How can I repeat the pattern [0-9]+\; two times, followed by [0-9]+ ?

Last edited by Faki; 05-03-2022 at 04:57 PM.
 
Old 05-03-2022, 05:19 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Faki View Post
Figured that it might be interpreted as \033[21;41;32;0m. This means that I cannot do
Code:
if [[ $1 =~ \[ ([0-9]+\;){3}m ]]
How can I repeat the pattern [0-9]+\; two times, followed by [0-9]+ ?
Did you read post #2, where you got an exact answer??? Did you read the link from pan64, which has all the information you need??? Did you actually try the solution you were given??
 
Old 05-04-2022, 07:10 AM   #8
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Want to see if post #2 could be shortened.
 
Old 05-04-2022, 07:35 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
just tell me why did you insert a space inside?
Code:
if [[ $1 =~ \[ ([0-9]+\;){3}m ]]
              ^
 
Old 05-04-2022, 08:10 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Faki View Post
Want to see if post #2 could be shortened.
Possibly can; so since you have the documentation and a good starting point, why don't YOU do it, since it's what you want to do??? Why are we doing it for you?
 
Old 05-04-2022, 03:25 PM   #11
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
You are right, I put a space, which should be there.
 
  


Reply

Tags
regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell: how to escape (or not escape) $ within an echo statement? Paul_N Linux - Newbie 2 04-05-2016 01:59 AM
Sending escape codes through CUPS Tracker77 Linux - Desktop 1 02-10-2008 11:45 AM
Escape Codes for Console Color trainpic Linux - General 1 03-13-2006 07:45 PM
Using escape codes to print barcodes c2tidis Linux - Software 0 09-29-2005 11:44 AM
Can Java detect keyboard escape codes? pdibona Programming 1 10-13-2002 08:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration