0

can you help me with this:

I want a regular expression for my Ruby program to match a word with the below pattern

Pattern has

  1. List of letters ( For example. ABCC => 1 A, 1 B, 2 C )
  2. N Wild Card Charaters ( N can be 0 or 1 or 2)
  3. A fixed word (for example “XY”).

Rules:

  1. Regarding the List of letters, it should match words with

    a. 0 or 1 A

    b. 0 or 1 B

    c. 0 or 1 or 2 C

  2. Based on the value of N, there can be 0 or 1 or 2 wild chars
  3. Fixed word is always in the order it is given.
  4. The combination of all these can be in any order and should match words like below

ABWXY ( if wild char = 1)

BAXY

CXYCB

But not words with 2 A’s or 2 B’s

I am using the pattern like ^[ABCC]*.XY$

But it looks for words with more than 1 A, or 1 B or 2 C's and also looks for words which end with XY, I want all words which have XY in any place and letters and wild chars in any postion.

4 Answers 4

2

If it HAS to be a regex, the following could be used:

if subject =~ 
    /^                          # start of string
    (?!(?:[^A]*A){2})           # assert that there are less than two As
    (?!(?:[^B]*B){2})           # and less than two Bs
    (?!(?:[^C]*C){3})           # and less than three Cs
    (?!(?:[ABCXY]*[^ABCXY]){3}) # and less than three non-ABCXY characters
    (?=.*XY)                    # and that XY is contained in the string.
    /x

    # Successful match
else
    # Match attempt failed
end

This assumes that none of the characters A, B, C, X, or Y are allowed as wildcards.

1

I consider myself to be fairly good with regular expressions and I can't think of a way to do what you're asking. Regular expressions look for patterns and what you seem to want is quite a few different patterns. It might be more appropriate to in your case to write a function which splits the string into characters and count what you have so you can satisfy your criteria.

Just to give an example of your problem, a regex like /[abc]/ will match every single occurrence of a, b and c regardless of how many times those letters appear in the string. You can try /c{1,2}/ and it will match "c", "cc", and "ccc". It matches the last case because you have a pattern of 1 c and 2 c's in "ccc".

One thing I have found invaluable when developing and debugging regular expressions is rubular.com. Try some examples and I think you'll see what you're up against.

I don't know if this is really any help but it might help you choose a direction.

1

You need to break out your pattern properly. In regexp terms, [ABCC] means "any one of A, B or C" where the duplicate C is ignored. It's a set operator, not a grouping operator like () is.

What you seem to be describing is creating a regexp based on parameters. You can do this by passing a string to Regexp.new and using the result.

An example is roughly:

def match_for_options(options)
  pattern = '^'

  pattern << 'A' * options[:a] if (options[:a])
  pattern << 'B' * options[:b] if (options[:b])
  pattern << 'C' * options[:c] if (options[:c])

  Regexp.new(pattern)

end

You'd use it something like this:

if (match_for_options(:a => 1, :c => 2).match('ACC'))
  # ...
end
2
  • A, B, C, C, wild-char, (XY). In any order and any position. The pattern you mentioned would match for ACC but not for CAC or CA. How to tell to look for the letters and wild-chars in all combinations?
    – DV Dasari
    Nov 2, 2010 at 20:02
  • Regular expressions are sometimes tricky things to design properly, so having a good reference on the various tools at your disposal within the system is a good idea. Rubular is a great place to experiment with potential expressions, but it may take some time to get the pattern you want. Sometimes the solution is not a regular expression but a tokenizer or a parser that's a lot more robust. I'm still not sure what patterns you want to describe, so that code above is merely an example of one technique.
    – tadman
    Nov 3, 2010 at 0:01
1

Since you want to allow these "elements" to appear in any order, you might be better off writing a bit of Ruby code that goes through the string from beginning to end and counts the number of As, Bs, and Cs, finds whether it contains your desired substring. If the number of As, Bs, and Cs, is in your desired limits, and it contains the desired substring, and its length (i.e. the number of characters) is equal to the length of the desired substring, plus # of As, plus # of Bs, plus # of Cs, plus at most N characters more than that, then the string is good, otherwise it is bad. Actually, to be careful, you should first search for your desired substring and then remove it from the original string, then count # of As, Bs, and Cs, because otherwise you may unintentionally count the As, Bs, and Cs that appear in your desired string, if there are any there.

You can do what you want with a regular expression, but it would be a long ugly regular expression. Why? Because you would need a separate "case" in the regular expression for each of the possible orders of the elements. For example, the regular expression "^ABC..XY$" will match any string beginning with "ABC" and ending with "XY" and having two wild card characters in the middle. But only in that order. If you want a regular expression for all possible orders, you'd need to list all of those orders in the regular expression, e.g. it would begin something like "^(ABC..XY|ACB..XY|BAC..XY|BCA..XY|" and go on from there, with about 5! = 120 different orders for that list of 5 elements, then you'd need more for the cases where there was no A, then more for cases where there was no B, etc. I think a regular expression is the wrong tool for the job here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.