سؤال

I need a ruby regex to create 11 groups separated by underscores, but to not match at all if there are more than 11 underscores. I also need consecutive underscores to be understood as an "empty" group. Here's what I have so far, which fails to address the > 11 underscores issue:

/^(dw|lat)\_(.*)\_(Paid-Search|Text-Ad)\_(.*)\_(.*)\_(.*)\_(.*)\_(.*)\_(.*)\_(.*)\_(.*)$/

Here are a couple example test cases.

Should match:

lat_march madness update_Paid-Search_subscription-one_google_ncaa-tournament_{adid}_p__March172014_2

but not match:

lat_los angeles_Paid-Search_nami-media_adn_JgYno0gS7yYjNq8OT7n_LcgTN9nt6vrmbC9qlcp__-21150_49996_7006_April22014_4

هل كانت مفيدة؟

المحلول 2

Change your (.*) to ([^_]*). This will create a character class of anything but _, and match it 0+ times. This way _ never gets counted as a capture group, but only a delimiter. Also, _ is not a reserved character so it does not need to be escaped. Final expression:

^(dw|lat)_([^_]*)_(Paid-Search|Text-Ad)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)$

Update:

If you don't need your last 8 capture groups to be matched, you can clean up this expression even further:

^(dw|lat)_([^_]*)_(Paid-Search|Text-Ad)(?:_[^_]*){8}$

This just takes the last pattern and repeats it 8 times. However, if you try to capture the [^_]*, it will only remember the last occurrence as your 4th capture group (rather than remembering all 8).

نصائح أخرى

Must you have a regex? Here's a simple non-regex solution.

Code

(arr = str.split('_')).size == 11 ? arr : nil

Examples

str = "lat_march madness update_Paid-Search_subscription-" +
        "one_google_ncaa-tournament_{adid}_p__March172014_2"

(arr = str.split('_')).size == 11 ? arr : nil
  #=> ["lat", "march madness update", "Paid-Search", "subscription-one",
  #    "google", "ncaa-tournament", "{adid}", "p", "", "March172014", "2"]

str = "lat_los angeles_Paid-Search_nami-media_adn_JgYno0gS7yYjNq8OT7n_" +
        "LcgTN9nt6vrmbC9qlcp__-21150_49996_7006_April22014_4"
(arr = str.split('_')).size == 11 ? arr : nil
  #=> nil
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top