Need help with Regex for bash | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Need help with Regex for bash

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 26 August 2014, 11:44 AM EDT
Posts: 167
Thanks: 75
Thanked 12 Times in 2 Posts
Need help with Regex for bash

Hi, I am trying to match this word: hexagon-bx.mydomain.com with regex. I have tried this:


Code:
"[a-z0-9\-\[bu]\.*]*$"

So far I have not been successful. I also need to make sure that the regex will match words that just have lowercase letters and numbers in them, such as camera01. How can I create such an expression? I am trying to do a expr match on "[a-z0-9\-\[bu]\.*]*$" so that it would pick up words like hexagon-bx/mydomain.com. Any help is greatly appreciated!

Last edited by radoulov; 12-30-2012 at 08:09 AM..
Sponsored Links
    #2  
Old 12-30-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 3:01 PM EDT
Location: Aachen, Germany
Posts: 4,159
Thanks: 69
Thanked 994 Times in 942 Posts
What you proposed will match your requirement. But it will match almost anything else that does not have an upper case or punctuation char at the end. What do you want to discriminate it against? Pls post input and desired output, and the command that you want to run.

Last edited by RudiC; 12-30-2012 at 09:03 AM.. Reason: typo
Sponsored Links
    #3  
Old 12-30-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 6:34 PM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
A regex that would do that can range from simple and broad to complicated and precise. It depends on the requirements. Let's start with simple:

Code:
^[a-z0-9.-]*$

To also match the slash:

Code:
^[/a-z0-9.-]*$


Last edited by Scrutinizer; 12-30-2012 at 07:46 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
newbie2010 (12-30-2012)
    #4  
Old 12-30-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 7:19 PM EDT
Location: San Jose, CA, USA
Posts: 4,484
Thanks: 177
Thanked 1,506 Times in 1,278 Posts
As Rudi says, we need to know how a regular expression is going to be used to be able to provide you with a regular expression that will match anything. If there is a file containing the strings:

Code:
hexagon-bx.mydomain.com
camera01
hexagon-bx/mydomain.com

it is true that the argument "[a-z0-9\-\[bu]\.*]*$" can be given to grep as a BRE or to grep -E as an ERE to match all three lines.

But, I get the feeling that what is being requested may be a pattern match rather than a BRE or ERE. And nothing that has been suggested will cause the command:

Code:
echo expression

to print the pathnames hexagon-bx.mydomain.com , camera01 , nor hexagon-bx/mydomain.com if they are pathnames of existing files in and under the current directory.

We need more context to help.

Last edited by Don Cragun; 12-30-2012 at 07:23 AM.. Reason: Mistaken sentence removed...
Sponsored Links
    #5  
Old 12-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 26 August 2014, 11:44 AM EDT
Posts: 167
Thanks: 75
Thanked 12 Times in 2 Posts
Solved - Regex

Thanks, Scrutinizer! I was actually trying to use if expr match "$xyz" "[a-z0-9.-] and the post you gave worked! $xyz was a variable assigned to hexagon-bu.mydomain.com. The only questions I now have is

1. Does expr match use regex differently than egrep?
2. Why do we not need to escape the period or the dash in the [z-a0-9.*] I am not sure why we would not need to escape these characters as I am relatively a newbie to regex.

Thanks for everyone's assistance and input!
Sponsored Links
    #6  
Old 12-30-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 7:19 PM EDT
Location: San Jose, CA, USA
Posts: 4,484
Thanks: 177
Thanked 1,506 Times in 1,278 Posts
No, expr and egrep do not use the same types of regular expression. Although some man pages aren't very clear about what form of regular expressions are used by expr match string pattern and expr string : pattern , the standards are clear:

Code:
       The  ':'  matching operator shall compare the string resulting from the
       evaluation of expr1 with the regular expression pattern resulting  from
       the  evaluation	of  expr2.  Regular  expression  syntax  shall be that
       defined in the Base Definitions volume of IEEE Std 1003.1-2001, Section
       9.3,  Basic  Regular Expressions, except that all patterns are anchored
       to the beginning of the string (that is, only sequences starting at the
       first character of a string are matched by the regular expression) and,
       therefore, it is unspecified whether '^' is a special character in that
       context.  Usually,  the	matching operator shall return a string repre-
       senting the number of characters matched ( '0'  on  failure).  Alterna-
       tively,	if the pattern contains at least one regular expression subex-
       pression "[\(...\)]" ,  the  string  corresponding  to  "\1"  shall  be
       returned.

Note that the match string pattern form is not in the standards; it is an extension supported on some (but not all) systems.

The egrep (which is obsolete) and the equivalent grep -E commands use extended regular expressions. The description of grep 's regular expressions in POSIX is:

Code:
Regular expression matching shall be based on text lines. Since a <newline> separates or
terminates patterns (see the −e and −f options below), regular expressions cannot contain a
<newline>. Similarly, since patterns are matched against individual lines (excluding the
terminating <newline> characters) of the input, there is no way for a pattern to match a
<newline> found in the input.

and
Code:
       -E     Match using extended regular  expressions.  Treat  each  pattern
	      specified as an ERE, as described in the Base Definitions volume
	      of IEEE Std 1003.1-2001, Section 9.4, Extended  Regular  Expres-
	      sions.   If any entire ERE pattern matches some part of an input
	      line excluding the terminating  <newline>,  the  line  shall  be
	      matched.	A null ERE shall match every line.

You don't need to backslash-escape characters in a bracket expression in an RE. If you do, the backslash character itself becomes a member of the set of characters to be matched by a matching bracket expression or to be excluded from the list of characters to be matched by a non-matching bracket expression.
The Following User Says Thank You to Don Cragun For This Useful Post:
newbie2010 (12-30-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Hi im new to bash scripting I want to know what does the regex expression do ?? kevin298 Shell Programming and Scripting 1 10-26-2012 10:50 PM
Bash regex help woodson2 Shell Programming and Scripting 12 10-10-2012 11:54 PM
[BASH] Allow name with spaces (regex) whyte_rhyno Shell Programming and Scripting 3 12-27-2011 06:47 PM
Bash regex kerloi Shell Programming and Scripting 4 04-18-2011 03:18 AM
regex test in bash subin_bala Shell Programming and Scripting 1 04-16-2008 03:27 AM



All times are GMT -4. The time now is 07:26 PM.