Match subpatterns in any order
Posted
by
Yaroslav
on Stack Overflow
See other posts from Stack Overflow
or by Yaroslav
Published on 2013-10-27T02:00:10Z
Indexed on
2013/10/27
3:54 UTC
Read the original article
Hit count: 245
I have long regexp with two complicated subpatters inside. How i can match that subpatterns in any order?
Simplified example:
/(apple)?\s?(banana)?\s?(orange)?\s?(kiwi)?/
and i want to match both of
apple banana orange kiwi
apple orange banana kiwi
It is very simplified example. In my case banana
and orange
is long complicated subpatterns and i don't want to do something like
/(apple)?\s?((banana)?\s?(orange)?|(orange)?\s?(banana)?)\s?(kiwi)?/
Is it possible to group subpatterns like chars in character class?
UPD Real data as requested:
14:24 26,37 Mb
108.53 01:19:02 06.07
24.39 19:39
46:00
my strings much longer, but it is significant part. Here you can see two lines what i need to match.
First has two values: length
(14 min 24 sec) and size
26.37 Mb.
Second one has three values but in different order: size
108.53 Mb, length
01 h 19 m 02 s and date
June, 07
Third one has two size
and length
Fourth has only length
There are couple more variations and i need to parse all values.
I have a regexp that pretty close except i can't figure out how to match patterns in different order without writing it twice.
(?<size>\d{1,3}\[.,]\d{1,2}\s+(?:Mb)?)?\s?
(?<length>(?:(?:01:)?\d{1,2}:\d{2}))?\s*
(?<date>\d{2}\.\d{2}))?
NOTE: that is only part of big regexp that forks fine already.
© Stack Overflow or respective owner