regular expression to find if a string contains all the characters of another string
-
Wednesday, July 04, 2012 1:14 AM
Hi
I need a regular expression to find if a string contains all the characters of another string.
Can anyone help me with this please?
I believe I've worked out the correct expression.
HERE IS THE ANSWER:
REPEAT .*[a,b,c]{1} as many times as the number of characters in the 2nd string and also replace [a,b,c] in the square bracket with all characters in the 2nd string.
- Edited by SQLearning Wednesday, July 04, 2012 1:46 AM worked out the answer myself
- Edited by SQLearning Wednesday, July 04, 2012 1:56 AM
All Replies
-
Wednesday, July 04, 2012 2:15 PM
Nope. First, the syntax is wrong. Second, matching .* will match anything. Third, there are no commas in the [abc] part. Fourth, even if you got this to work:
[abc][abc][abc][abc] This matches aaaa, which doesn't have all the characters of abc. So you're on the wrong track.
And finally, I don't believe you can write a regular expression to do such a thing.
-
Thursday, July 05, 2012 6:42 AMModerator
Hi SqlLearning,
Thank you for sharing your solution.
I believe I've worked out the correct expression.
HERE IS THE ANSWER:
REPEAT .*[a,b,c]{1} as many times as the number of characters in the 2nd string and also replace [a,b,c] in the square bracket with all characters in the 2nd string.Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, July 05, 2012 6:42 AM
-
Saturday, July 07, 2012 12:33 AM
I don't believe your regex will work since for example, your regex ".*[a,b,c]{1}" will match "Now is the time for all good men to come to the aid of their party"
You can certainly use a regex to pare your first string down to just a list of the unique letters in the string, by removing duplicates. I also chose to remove <spaces>:
(you need to set the SingleLine option so that dot matches newline)
(\S)(?=.*?\1)|\sOnce you have that list, you could iterate through the list one by one, checking to see that it exists in the second string.
Ron

