Would like to share some common regex patterns...
2 Attachment(s)
Hi All
Recently, as part of my job, i had to search for commonly known strings from some large text files. That job proved to be not that easy. After goggling for a day and searching many books for some good regex expressions, i came up with these regex patterns. Although, many experts may have already known/used these, i thought it may be of some help for newbies. Also, I, being a novice in scripting/regex, the scripts i create may not be the best but will sure get the job done. In that sense, i really appreciate if there are any tips to do this in a better way. This script matches almost all valid MAC, IP, URL and Email addresses. ~/.bashrc PHP Code:
PHP Code:
Code:
$ grep -Ei "$(MAC)" testfile Thanks. Note: Please remove .txt extension from attachments. |
I am a little confused as why you need functions? Could you not just set the variables equal to your regexes?
Also, why is it the email option is quite in depth but for urls apparently there seem very little restrictions? |
Quote:
Also, i thought by putting them in functions, i can use them on any commands like, Code:
$ ifconfig eth0 | grep -E "$(IP4)" Quote:
'\b[a-z0-9]{1,}@*\.(com|net|uk|mil|gov|edu)\b' But it did not capture complex emails with multiple dot/number inbetween them. So after googling for a while, i got a link that said there is regex The Official Standard: RFC 5322 for emails. so i used that. |
Quote:
Quote:
I would probably use functions if I wanted to use them in place of calling something like grep, like gmac to be for grep mac addresses. I would also add that as you are unlikely to add to this script to then use it as an actual script, there is no need to place the interpreter at the top of the file as it will always be used as a sourced file. So maybe something like: Code:
## Alias grep with color |
@grail
Thanks for responding. You're right, variables can be used instead. Its really funny why i didn't think about that. Probably because i was working continuously for 13 hrs stretch and it was 3 late midnight or simply this regex got on my nerves, i don't know. Anyway, i just booted my PC, i'm fresh and now i know what mistakes i did. Sorry for also confusing you and others and also dragging in other direction. I'm glad i posted this on LQ before posting it on my company's intranet. As you correctly suggested all i needed was to put it in variable and export it. This was all i needed... just put them in .bashrc ~/.bashrc Code:
alias grep='grep --color=auto' Code:
$ . ~/.bashrc |
All times are GMT -5. The time now is 10:05 PM. |