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.
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |