• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

A little help with regular expressions

Status
Not open for further replies.

asdftt123

Senior member
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.
 
I barely know what I'm talking about, but I would probably do it the lazy way and make if/then structured spaghetti code.
 
"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:
"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.
 
Edited my above post to show what each part of the expression means. Read through it so you understand what it's doing
 
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.
 
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.
Back
Top