Regex for for integer from 0-7 except 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex for for integer from 0-7 except 2
# 1  
Old 04-16-2010
Regex for for integer from 0-7 except 2

Hi,

I am trying to use a regular exp to match any number between 0 and 7 except 2......
I am using:

echo $myvar | egrep "{1}[0-1]{1}[3-7]"Which isnt working....

Please can someone help?
Thanks

yonderboy
# 2  
Old 04-16-2010
MySQL

This is the answer you are seeking

Code:
egrep -v "[289]"

# 3  
Old 04-16-2010
You can try in Sed
Code:
$cat file_name
0
1
2
3
4
5
6
7
sed -n '/2/n; /[0-7]/p' file_name
0
1
3
4
5
6
7


Last edited by posix; 04-16-2010 at 02:10 AM.. Reason: misprint 9 in place of 7
# 4  
Old 04-16-2010
Fantastic, thank you. My code is working.

---------- Post updated at 03:20 PM ---------- Previous update was at 03:08 PM ----------

Just out of interest, can you explain how the first solution works?
# 5  
Old 04-16-2010
The option -v make grep to search for everything that don't match the regexp, in this case, it prints every line that not contains chars 2, 8 or 9
# 6  
Old 04-16-2010
grep '[013-7]'

js.
# 7  
Old 04-16-2010
Quote:
Originally Posted by ungalnanban
This is the answer you are seeking

Code:
egrep -v "[289]"

Hahaha. this also matches everything else under the sun, including:

jdlkflkflkdl320kkjkjlk&%#jkvjkjfdfdfd

Smilie

.... which is not what the original poster asked for Smilie

---------- Post updated at 15:59 ---------- Previous update was at 15:53 ----------

The regex to match 0-7 but not any 2 is:
Code:
[013-7]

to match one or more of the above, use:
Code:
[013-7]+



Quote:
[013-7]+

Options: case insensitive; free-spacing

Match a single character present in the list below «[013-7]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
One of the characters “01” «01»
A character in the range between “3” and “7” «3-7»

Last edited by Neo; 04-16-2010 at 01:01 PM.. Reason: fixed typo (7 not 9)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Are These All The Integer Types In C

Hello and Good day, I am currently studying C and I just finished learning about variables mainly those of integer type. I am wondering if the list below are all there is to integer variables and there are still more that i have to learn. Here are the list: Char Short int long long long... (3 Replies)
Discussion started by: split_func0
3 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

4. Solaris

Integer Validation

Hi Using the below logic to check whether the value is integer or not and converting the value to decimal if not. int_chck = sprintf(substr($i, 1, 2)) o_cnt = 'if ; then echo $int_chck; else echo $((0x$int_chck)); fi' Thanks, Dines (4 Replies)
Discussion started by: dineshnak
4 Replies

5. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. UNIX for Dummies Questions & Answers

Non-integer caluclations

Hey I am trying to calculate a number but I found out the expr I knew works only with integers. Any help. I want to calculate (120/220) *100. Thanks! (2 Replies)
Discussion started by: #moveon
2 Replies

9. Shell Programming and Scripting

increment an integer

hi I want to echo the variable $i while it auto-increments till 21 I set initially i to 1 any idea how to do that? thank you (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

10. IP Networking

how to pass integer

i am writing a client and server program client program main() { int sockfd,n; char str; struct sockaddr_in sock; if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { perror("SOCKET ERROR"); } bzero(&sock,sizeof(sock)); sock.sin_family=AF_INET; (1 Reply)
Discussion started by: ramneek
1 Replies
Login or Register to Ask a Question