A little help with regular expressions

Status
Not open for further replies.

asdftt123

Senior member
Jul 27, 2007
612
0
76
I know very little CS but I'm force to come up with a regular expression that will accept either 0, 0.5, 1, 2, or 3. Can someone help? Thanks.
 

Kalmah

Diamond Member
Oct 2, 2003
3,692
1
76
I barely know what I'm talking about, but I would probably do it the lazy way and make if/then structured spaghetti code.
 

venkman

Diamond Member
Apr 19, 2007
4,950
11
81
I know absolutely zero CS

IF (x=0 OR X=0.5 OR x=1 OR X=2 OR X=3)
do stuff
ELSE
nef
 

DanFungus

Diamond Member
Jul 27, 2001
5,857
0
0
"rejects any other number" -- what kind of data are you expecting as input and what is the format of the data?

Here's my expression:
'^[0-3]$|^0\.5$'

This means: At the start of the line (^) there should be a 0, 1, 2, or 3 ([0-3]), and then be the end of the line ($) OR at the start of the line (^) there should be a 0.5 (\. means period, . by itself means any single character in this context) then the end of the line ($).

Here's my test file:
0
0.25
0.5
0.51
1
1.5
2
3
4
5
6
7

Here's the output:
0
0.5
1
2
3

But that won't work if they aren't on separate lines..
 
Last edited:

asdftt123

Senior member
Jul 27, 2007
612
0
76
"rejects any other number" -- what kind of data are you expecting as input and what is the format of the data?

Here's my expression:
'^[0-3]$|^0\.5$'

Here's my test file:
0
0.25
0.5
0.51
1
1.5
2
3
4
5
6
7

Here's the output:
0
0.5
1
2
3

But that won't work if they aren't on separate lines..

Wow! That's perfect, thanks! :) I don't think I could have ever figured that out on my own.
 

DanFungus

Diamond Member
Jul 27, 2001
5,857
0
0
Edited my above post to show what each part of the expression means. Read through it so you understand what it's doing
 

asdftt123

Senior member
Jul 27, 2007
612
0
76
Edited my above post to show what each part of the expression means. Read through it so you understand what it's doing

Yeah, thanks for the explanation. It makes a ton more sense now. I couldn't find any good tutorials online for writing these things and the last time I tried to write a regular expression for a blood pressure reading (i.e. 120/60) it took me a few hours. Thanks again.
 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
Yeah, thanks for the explanation. It makes a ton more sense now. I couldn't find any good tutorials online for writing these things

The best I'm familiar with is http://www.regular-expressions.info/ but even that can be very confusing at first. It helps a lot to pick up a text editor that can do good regex matching replacement, so you can more easily fine-tune things; I like Textpad for this purpose. Notepad++ also supports regular expressions (but I haven't really used the feature), and is an all-around good text editor.

and the last time I tried to write a regular expression for a blood pressure reading (i.e. 120/60) it took me a few hours. Thanks again.

m/(\d+)\/(\d+)/

In Perl, as all good regexes should be :)

I rely often on my own regular expression cheat-sheet (the PDF version hangs just above my monitor at work), and you might find it useful as well.
 
Status
Not open for further replies.