Learning regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Learning regular expressions
# 1  
Old 03-24-2014
Learning regular expressions

Hey, trying to learn more tricks with regular expressions, and got this one.

Best I could come up with for this problem is a "two-stepper" :

Code:
egrep -i "[a-zA-Z0-9]@[a-zA-Z0-9]" test.txt | egrep -v "@[a-zA-Z0-9]*.com"

Which does bring back the proper results. I'm interested, however, if this can be done with a single reg exp ?

Test file is:

Code:
quick brown fox@gmail.com jumped over
quick brown fox@il.com jumped over
lazy dog@over the hill
lazy dog@mail the hill
jack and @jill
ignore this line

Expected return is:

Code:
lazy dog@over the hill
lazy dog@mail the hill

Basically:
@xxx.com is an email, ignore it.
<space>@xxx isn't a pattern I want, ignore it.
no @ in line - don't want it, ignore it.

<string>@<string> is what I'm looking for - but not ending in .com.
(I just look for single character before and after, that way I know it has to be upper/lower case, or number only. No need to look for _, or other special characters.

In short: I'm searching for Oracle DB Links, but don't want email addresses, or SQL script calls.
# 2  
Old 03-24-2014
Code:
egrep '[a-zA-Z0-9]+@[a-zA-Z0-9]+ '

# 3  
Old 03-24-2014
FWIW -

egrep -v does an exclusionary search and using alternation try:

Code:
egrep -v  '( @[a-zA-Z0-9]|\.com )'  somefile

Just an opinion - sometimes really long one-liners are harder to debug. We see 250 character "one-liners" here frequently. They are hard to work with.

Last edited by jim mcnamara; 03-24-2014 at 12:25 PM..
# 4  
Old 03-24-2014
Quote:
Originally Posted by jim mcnamara
FWIW -

egrep -v does an exclusionary search and using alternation try:

Code:
egrep -v  '( @[a-zA-Z0-9]|\.com )'  somefile

Just an opinion - sometimes really long one-liners are harder to debug. We see 250 character "one-liners" here frequently. They are hard to work with.
Understood .. I'm just curious to use this as a learning exercise.

hmm, that's cute, reverse logic. Clever.
Ok, it still pulls back a line with no @'s in it.
Trying this, however, still doesn't work ... hmmm

Code:
egrep -v  '([^@]| @[a-zA-Z0-9]|\.com )' test.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

2. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

3. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

4. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

5. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

6. Shell Programming and Scripting

Need help with Regular Expressions

Hi, In ksh, I am trying to compare folder names having -141- in it's name. e.g.: 4567-141-8098 should match this expression '*-141-*' but, -141-2354 should fail when compared with '*-141-*' simlarly, abc should fail when compared with '*-141-*' I tried multiple things but nevertheless,... (5 Replies)
Discussion started by: jidsh
5 Replies

7. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

8. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

9. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question