osanger
osanger

Reputation: 2352

Regex: check multiple times in one string javascript

Hey I need a regex for the following cases:

- von 08:00-12:00uhr und 12:00-22:00 uhr
- von 08:00-12:00 uhr 
- von 08:00-12:00 uhr
- von 08:00-12:00
- 08:00-12:00

so the regex should handle input like that:

(von)?_?0?[0-23]:[00-59]_?(Uhr|uhr)?((bis)?_?0?[0-23]:[00-59]_?(Uhr|uhr))?

I tried a lot but the best match after reading in the wiki was:

(von)?([01]\d|2[0-3]):?([0-5]\d)$(uhr|Uhr)?((und)?(von)?([01]\d|2[0-3]):?([0-5]\d)$)

Can you give me an advise how i can handle this problem? Is the way i test the Dateformat right?

Upvotes: 0

Views: 56

Answers (3)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44720

How about this one:

/([0-2]\d:[0-5]\d\-[0-2]\d:[0-5]\d)/g

You can try it here: https://regex101.com/r/eD0hY1/1

You are only interested in the times, correct?

Upvotes: 0

ontime
ontime

Reputation: 123

I'm assuming that your first example is an error and it should be

von 08:00-12:00 uhr und 12:00-22:00 uhr

instead of

von 08:00-12:00uhr und 12:00-22:00 uhr

This is a quick solution I came up with. There is a place for improvement though, using the space character instead of ' ' (space).

(von )?(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]-(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]( uhr)?( und (0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]-(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9] uhr)?

Note: the solution is based on the top voted answer on Regular expression for matching HH:MM time format

Upvotes: 0

L3viathan
L3viathan

Reputation: 27273

This seems to work:

(von )?([01]\d|2[0-3]):[0-5]\d-([01]\d|2[0-3]):[0-5]\d( ?uhr)?( und ([01]\d|2[0-3]):[0-5]\d-([01]\d|2[0-3]):[0-5]\d( ?uhr)?)?

Spaces are matched by an actual space character, \s matches not just the ascii space, but most kinds of whitespace (e.g. also tabs).

Upvotes: 1

Related Questions