Finding time through regex -


i need find retrieve time given text e.g. join dinner tonight til 10pm #lebunsocial @3compasses #e8 #dalston

for above condition below code works fine

  if (regex.ismatch(str, @"(?'hour'\d{2})(?'ampm'am|am|pm|pm|pm)", regexoptions.compiled)) //2 digits + pm/am" (without space)         {             m = regex.match(str, @"(?'hour'\d{2})(?'ampm'am|am|pm|pm|pm)", regexoptions.compiled);             result = true;             counter++;         } 

but due business reasons have execute below code well. regex should not match 10pm 1 digit restriction mentioned below still matches below regex. how avoid ? below code should work (for e.g. 1pm) 1 digit

 if (regex.ismatch(str, @"(?'hour'\d{1})(?'ampm'am|am|pm|pm|pm)", regexoptions.compiled)) // 1 digit + am/pm without space         {             m = regex.match(str, @"(?'hour'\d{1})(?'ampm'am|am|pm|pm|pm)", regexoptions.compiled);             result = true;             counter++;         } 

\d (the {1} no-op because every regex token matched once unless otherwise specified) matches 0 in 10. if want match single digits, can use negative lookbehind assertion:

@"(?<!\d)(?'hour'\d)(?'ampm'am|am|pm|pm|pm)" 

but entire approach weird. example, why not make regex case-insensitive (or want allow pm , disallow am)?

furthermore, why match against same regex twice?


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -