Unix Shell - A grep regular expression

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > A grep regular expression





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author A grep regular expression
Steven Jones

2006-10-24, 7:17 pm

I am looking for a grep regular expression to match strings that
start with a given character, say, A, and end with another
character, say, B; but, only if all the characters between A and B are
either letters (either case) or decimal integers.


Janis Papanagnou

2006-10-24, 7:17 pm

Steven Jones wrote:
> I am looking for a grep regular expression to match strings that
> start with a given character, say, A, and end with another
> character, say, B; but, only if all the characters between A and B are
> either letters (either case) or decimal integers.
>
>


egrep '(A[[:alpha:]]*B|A[[:digit:]]*B)'

You may also call it as grep -E '(...)'

Janis
Xicheng Jia

2006-10-24, 7:17 pm

Steven Jones wrote:
> I am looking for a grep regular expression to match strings that
> start with a given character, say, A, and end with another
> character, say, B; but, only if all the characters between A and B are
> either letters (either case) or decimal integers.


Is 'AA12345B' a match???

If 'yes':
{
grep -i 'A\([A-Z]*\|[0-9]*\)B'
grep -Ei 'A([A-Z]*|[0-9]*)B'

} else {

you may want to add some boundary constraints, like:

grep -Ei '(^| )A([A-Z]*|[0-9]*)B( |$)'

}

(tested under GNU grep)
Regards,
Xicheng

Chris F.A. Johnson

2006-10-24, 7:17 pm

On 2006-10-24, Steven Jones wrote:
> I am looking for a grep regular expression to match strings that
> start with a given character, say, A, and end with another
> character, say, B; but, only if all the characters between A and B are
> either letters (either case) or decimal integers.


^A[a-zA-Z0-9]*B$

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Kaz Kylheku

2006-10-25, 1:32 am

Janis Papanagnou wrote:
> egrep '(A[[:alpha:]]*B|A[[:digit:]]*B)'
>
> You may also call it as grep -E '(...)'


Indeed, you may use a blessed modern command in place of an obsolescent
one.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com