|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|