Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression 1

Status
Not open for further replies.

TipGiver

Programmer
Sep 1, 2005
1,863
Well i havent found yet how to do the following with a regex. So here it is:

Allowed letters are A,B,C,D and E. How can i match exaclty three different letters?

Example: ABC, DEB etc should match. The next ones shound not! ABB (B is 2 times), ABF ('F' is out of A-B), ABCD (there are four, not three letters)

Thanks for any help on this!
 
Like this.
[tt] (?<![ABCDE])([ABCDE])(?!\1)([ABCDE])(?!\1|\2)([ABCDE])(?![ABCDE])[/tt]
 
Hi ZmrAbdulla, unfortunalety it has to be done with regex. If i could write code for it, then it wouldnt be a problem!

Tsuji, I'll try it and let you know what happened !


Thank you
 
[1] I can draft a quick c# console app to demo. You can port it to vb.
[tt]
using System.Text.RegularExpressions;
public class X {
public static void Main() {
string s;
s="DCB12123ABCii12123ABAii12123CBAiiABCD12123AACiiBAC";
string pattern;
pattern=@"(?<![ABCDE])([ABCDE])(?!\1)([ABCDE])(?!\1|\2)([ABCDE])(?![ABCDE])";

System.Console.WriteLine("[string to match:] "+s);
System.Console.WriteLine("[pattern:] "+pattern);
System.Console.WriteLine ("[IsMatch:] "+(new Regex(pattern)).IsMatch(s).ToString());

int i=1;
foreach (Match m in Regex.Matches(s, pattern)) {
System.Console.WriteLine("[particle #"+i+"] "+m.ToString());
i++;
}
}
}
[/tt]
[2] The output is this.
[tt]
[string to match:] DCB12123ABCii12123ABAii12123CBAiiABCD12123AACiiBAC
[pattern:] (?<![ABCDE])([ABCDE])(?!\1)([ABCDE])(?!\1|\2)([ABCDE])(?![ABCDE])
[IsMatch:] True
[particle #1] DCB
[particle #2] ABC
[particle #3] CBA
[particle #4] BAC
[/tt]
[3] If that's not what you meant to match, I am not going back to read again your description. It is that long in the past for a simple test like that.
 
Hello tsuji,

Dont get angry (your [3]). I dont remember to have asked for any code, just help for the regex. You could have said that it works fine for you and dont get into trouble writting C# code.

Anyway, i needed the regex to test it with the metatool "FLEX" (fast lexical analyser) that uses and generates ANSI C/C++ code. Maybe there is no compatibility between each regex.


Thanks for your insterest.
 
Hello again people,
the regex is fine. I tested outside flex.
Is it possible to explain me how this regex works? I do know that [ABCDE] is same as [A-E] which is a character class and matches only one of the 5 chars. ? means 0 or 1 instance. \n where 'n' here is 1 and 2, matches the 'n'th regex inside ().

If i am not wrong (?<![ABCDE]) is \1 and ([ABCDE]) is \2.
What is the use of the last part '([ABCDE])(?![ABCDE])' ?

I would appreciate if anyone spent 1 minute to explain (in short)
 
[ul]
[li]Start matching a character in the character set [A-E].[/li]
[li]To accept that character:
[ul][li]it must be in [A-E][/li][li]it must not be preceded (negative look-behind) by a character in [A-E]
[/li][li]It must not neither be followed (negative look-ahead) by the letter [A-E] as the one (bracketed for reference) which just matched up (\1).[/li][/ul][/li]
[li]Then the 2nd character: apart being in [A-E], it must not followed (negative look-ahead) by either the previously matched character (back-reference number 1 (\1)) or this 2nd character (bracketed for back-reference, which the number 2 (\2)).[/li]
[li]Then it is the 3rd character: it has to be followed by anything but a character in the set [A-E].[/li]
[/ul]
The pattern practically makes full use of all the major arsenal available for fine-grained matching.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top