LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-10-2012, 03:46 AM   #1
ksmatthews
Member
 
Registered: Aug 2008
Posts: 86

Rep: Reputation: 15
Question regex expression


Hi All,

I came across this regex '~^[a-zA-Z0-1/_.-]*$~'

I understand '^[a-zA-Z0-1/_.-]*$' but not '~^[a-zA-Z0-1/_.-]*$~'

What does it mean ?

Also sed expressions can use a 1 in the second part like

sed '/string1/\1/g'

What does the 1 mean ?

regards,

Steven M
 
Old 01-10-2012, 03:58 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
For the first part, it's possible that the '~' are just delimeters. In some versions of regex, the following are all identical:

Code:
s@a@b@
s/a/b/
s~a~b~
With regards to your second question, the \1 refers to the first group matched.

Code:
s/\(abc\)de/\1/g
will replace all occurences of 'abcde' with 'abc', because 'abc' is the first group found in the expression. Normally, you wouldn't have a fixed group, but something more along the lines of '[a-zA-Z]+ ' - so you can replace the \1 with the word which the previous expression matched.

Hope this helps,
 
Old 01-10-2012, 04:04 AM   #3
ksmatthews
Member
 
Registered: Aug 2008
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks Snark

RE Normally, you wouldn't have a fixed group, but something more along the lines of '[a-zA-Z]+ ' - so you can replace the \1 with the word which the previous expression matched.

What do you mean by this ? Can you offer a simple example ?

regards,

Steven M
 
Old 01-10-2012, 04:12 AM   #4
ksmatthews
Member
 
Registered: Aug 2008
Posts: 86

Original Poster
Rep: Reputation: 15
Hi Snark,

Consider this script fragment ...

IGNOREDB="#mysql#myweb#intranet#bandbusnet#information_schema#drupal#joomla#";
NOW=`date +%Y-%m-%d`;
BKPDIR=/var/log/mysql/$NOW;
MUSER="root"
MPASS="root"
MHOST="localhost"
MPORT="3306"
mkdir -p $BKPDIR;

DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')"

for DB in $DBS
do

if [[ "${IGNOREDB}" =~ "#${DB}#" ]]
then
continue;
fi


Here, the tilde is used in a different way ...

What does if [[ "${IGNOREDB}" =~ "#${DB}#" ]] mean ?

Why not use if [[ "${IGNOREDB}" = "#${DB}#" ]] ?


Steven M
 
Old 01-10-2012, 05:29 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by ksmatthews
Here, the tilde is used in a different way ...

What does if [[ "${IGNOREDB}" =~ "#${DB}#" ]] mean ?

Why not use if [[ "${IGNOREDB}" = "#${DB}#" ]] ?
Have a look at the bash man page (man bash). Quoting:
Code:
       [[ expression ]]
              Return  a  status  of  0 or 1 depending on the evaluation of the
              conditional expression expression.
    <snip>
              An  additional  binary operator, =~, is available, with the same
              precedence as == and !=.  When it is used,  the  string  to  the
              right  of the operator is considered an extended regular expres‐
              sion and matched accordingly (as in regex(3)).  The return value
              is 0 if the string matches the pattern, and 1 otherwise.
    <snip>
What may not be clear from that small clip is that the test will return 0 (a match) even if the match is on a substring. For instance:
Code:
user@localhost$ if [[ "abcdefg" =~ "cde" ]] ; then echo "match"; fi
match
user@localhost$ if [[ "abcdefg" =~ "cke" ]] ; then echo "match"; fi
user@localhost$
Knowing that, the script uses =~ as a substring match to locate any databases that are included in the $IGNOREDB variable. If the script had used == instead, the contents of "#${DB}#" would have had to be a verbatim copy of $IGNOREDB for the if statement to trigger.

As you can see from an earlier part of the script:
Code:
DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')"

for DB in $DBS
do
the variable $DB cycles through the results of the 'show databases' command one database at a time. Therefore, it would not be possible for "#${DB}#" to ever equal the full string in "${IGNOREDB}"

Quote:
Originally Posted by ksmatthews
RE Normally, you wouldn't have a fixed group, but something more along the lines of '[a-zA-Z]+ ' - so you can replace the \1 with the word which the previous expression matched.

What do you mean by this ? Can you offer a simple example ?
Imagine I have the following text file (named relationships.txt):
Code:
Uncle: Brian
Mother: Stacy
Brother: Mike
Observe the output of the following command:
Code:
user@localhost$ sed 's@\([a-zA-Z]\+\): \([a-zA-Z]\+\)@\2 is my \1@' relationships.txt
Brian is my Uncle
Stacy is my Mother
Mike is my Brother
The command works like this:
1. It identifies a sequence of lowercase and/or uppercase letters (stored in "memory" as the 1st group)
2. followed by a colon and a space
3. followed by another sequence of lowercase and/or uppercase letters (stored in "memory" as the 2nd group).
4. Replace whatever matched the regular expression with the the "memory" of the 2nd group (the '\2')
5. followed by " is my "
6. followed by the "memory" of the 1st group (the '\1')

Last edited by Dark_Helmet; 01-10-2012 at 05:39 AM.
 
Old 01-11-2012, 10:43 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Hopefully Dark Helmet answered all your questions - if you have any further ones feel free to ask

Just a couple of pointers (for the forum, not your code ):

You can use {QUOTE}{/QUOTE} to quote something I said that you want to reply to, rather than using 'RE:' (use square brackets, [], not angle brackets, {}, around the QUOTE).

Also, if you want to add something after you've already posted, you can click 'edit' to edit your post and add the new stuff on the end - it's considered politer than double-posting...

Thanks,
 
  


Reply

Tags
regular expression


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
what regex expression replaces file extension using bash script jdavis2 Linux - General 5 01-14-2010 05:39 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM


All times are GMT -5. The time now is 02:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration