0

Having two strings as parameters (s1, s2) I should be able to set up a new Regex(my_regular_expression(s1, s2)). For example s1="abcd", s2="xyz" I would like to match strings:

regex.IsMatched(x)==true, where x is one of the following:

abcd.xyz
abcd-xyz
xyzabcd
dxy
yzab
z a
dx
cd
but not limited to

but regex.IsMatched(y)==false, where y is one of the following:

aabcd.xyzv
abd.xyz
xycd
but not limited to

Between s1 and s2 there could be nothing or any character. Any right substring (see the function string.right(string str,int length) ) of s1 concatenated with a left substring (see the function string.left(string str,int length) ) of s2 or any right substring of s2 concatenated with a left substring of s1.

Please use s1 and s2 in regex not abcd, xyz. s1/s2 can contain special characters.

Thank you in advance.

algorytmus
  • 953
  • 2
  • 9
  • 28
  • Why would `dxy` match but not `aabcd.xyzv`? What about `xycd`? Can you update the question with a more complete definition of what the regex is supposed to match? – Ryan Gates Mar 20 '13 at 14:17

2 Answers2

1

I believe Combined would give you the expression you want. It has the following limitations.

  • It currently allows any character to combine the two halves (the . or - in your examples), even if it's a letter.
  • SuffixPart does not support Unicode characters which do not fit in a single UTF-16 code point. If you need to handle this edge case, here is a related question that talks about Java code.
  • For that matter PrefixPart also does not support Unicode characters which are not a single UTF-16 code point, but that method is not broken "as badly".

Here is the code:

public static string PrefixPart(string str)
{
    return
        string.Join("(?:", str.Select(i => Regex.Escape(i.ToString())))
        + string.Join(")?", Enumerable.Repeat(string.Empty, str.Length));
}

public static string SuffixPart(string str)
{
    return PrefixPart(new string(str.Reverse().ToArray()));
}

public static string Combined(string str1, string str2)
{
    string left = SuffixPart(str1) + ".?" + PrefixPart(str2);
    string right = SuffixPart(str2) + ".?" + PrefixPart(str1);
    return string.Format("^{0}|{1}$", left, right);
}
Community
  • 1
  • 1
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
0

I would just do

"/" + s1 + ".?" + s2 + "|" + s2 + ".?" + s1 + "/"

This would evaluate to (in your example):

/abcd.?xyz|xyz.?abcd/

That is both required patterns in either order separated by zero or one of any character.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405