0

I'd like to find the 30, 12/1, 2, 3 and so on, in some calendar data I'm parsing from html, but not ‍2014‍‍ or 09:30

So far I've tried

[\d]{1,2}[/]?[\d]{1,2}[^:]

and a lot of different variations, but nothing seems to fit. Is there a way to find the desired dates with one regex, or would it make more sense to ask either for a number between0 - 31 or a string containig numbers and /? Thanks for any help :)

Edit:

The data I'm parsing looks like this:

30
Name, Ofsomeone
Name, Ofsomeone
Name, Ofsomeone
weg.ics
GMT+01:00Amsterdam,Berlin,Bern,Rome,Stockholm,Vienna
weg:December2014
Day
WorkWeek
Week
Month
Today
December2014
December2014
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
12/1
Name, Ofsomeone
Name, Ofsomeone
Name, Ofsomeone
2
Name, Ofsomeone
Name, Ofsomeone
3
Name, Ofsomeone
Name, Ofsomeone
2
  • Could you post a snippet of the HTML that you are trying to parse? Commented Nov 18, 2014 at 21:47
  • Please add a list of string that your regex should match and what should not. What have you tried? Commented Nov 18, 2014 at 21:48

2 Answers 2

2

It sounds like the rule you want is: a number from 1 to 31, as a word by itself (not part of a date, time, 4-digit number, etc.), or a date in m/d format.

Let's assume you're willing to accept any 1- or 2-digit number, and not try to reject 75 or 38/2 or 2/30 in the regex itself.

  • You've got the [\d]{1,2} parts—although there's really no need for the brackets there.
  • We could write this as an alternation between two separate patterns, but it's a little more interesting to write it with an optional / and number, so let's do that.
  • To make it optional, we need something to attach the ? modifier to, which means a group. And presumably you don't want to capture the day separately, so it's a non-capturing group.
  • To handle the "by itself", since that seems to actually mean a line by itself, we can just use multiline mode and ^ and $ the pattern.

So:

^\d{1,2}(?:/\d{1,2})?$

Regular expression visualization

Debuggex Demo

If you did want to write the 1-31 into the regex, you'd probably want to change this to an optional 1-12 and slash, followed by a 1-31, which is going to be a big mess (^(?:(?:\d|(?:1[0-2]))/)?(?:(?:[12]?\d)|(?:3[01]))$); if you want to also handle the 30 and 29 depending on the first value… just don't even try that.


This the original answer to the different question, as originally stated.

It sounds like the rule you want is: two (1-or-2-digit) numbers separated by a hyphen or a slash with optional whitespace.

So, just translate that to re syntax:

  • You've got the [\d]{1,2} parts—although there's really no need for the brackets there.
  • You want to match either a hyphen or a slash, not a slash or nothing, so change the [/]? to [-/].
  • You want to allow spaces around the hyphen or slash, so add a \s* on each side of it.
  • I don't know why you're trying to exclude a following colon, because 09:30 is already not going to match the pattern.

So:

\d{1,2}\s*[-/]\s*\d{1,2}

Regular expression visualization

Debuggex Demo

Notice that it matches all of 1 - 31, 10/27, 11/1, 12/1, and does not match 2014 or 09:30.

Sign up to request clarification or add additional context in comments.

3 Comments

I looks like the OP wants ` - ` (space hyphen space) or / (just slash) in the middle. Although your solution is more flexible...
@iCodez: Now that he's provided input, there are no values with hyphens at all, spaces or otherwise, and no values with slashes with spaces to let us know whether they should match or not, so… really, this is still just a guess at what he wants…
Sorry for my failure, i'm looking for a number between 1 and 31. There is and should not be any hyphen or space in the string im parsing :/ Editing my Question. But thanks anyway for your explanation. :) Still learning regex and advice are always welcome :)
0

this could work for you :

[\d]{1,2} ?([^:][\/]|-) ?[\d]{1,2}

Regular expression visualization

Debuggex Demo

Note : we need a sign between tow [\d]{1,2} that spaces after the first one and second one could be optional that we do it with poting ? after space and in center of pattern that could not be : ([^:]) and could be / ([\/]) or (|) - .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.