Solved

regex to match exact number of occurence of a sequence

Posted on 2010-11-12
19
844 Views
Last Modified: 2012-05-10
Hello Experts,

I try to build a regex to match an exact number of occurence of a sequence/word :

For example "abc" and the number of occurence needed "3" and only three, no more no less

So :
abcXYZabcEFGabc match
abcXYZabcEFGabcKLMabc don't match because we find four time abc
abcXYZabcEFG don't match, only two is found
abcabcabc match

I have this : /^([^a]*a[^a]*){3}$/ but it work because I'm looking for "a" and not for a sequence "ab", "JPQ"

Thanks for your help!

Kinds Regards.
0
Comment
Question by:leakim971
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 25 total points
ID: 34124152
Does it have to be RegExp?

I don't know what specific language you are using, but if you:

1) Replace "abc" with "" and

2) The resulting string is 9 characters fewer in length than the original string

then you would have a "match".
0
 
LVL 82

Author Comment

by:leakim971
ID: 34124221
Thanks a lot for your reply.
The language is Javascript and It sound stupid but I need to use a regexp.

Something like : /^([^a]*a[^a]*){3}$/.test("abcXYZabcEFGabc") will return true or false



0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34124293
Why must it be RegExp?
0
 
LVL 1

Assisted Solution

by:phr0ze
phr0ze earned 25 total points
ID: 34124334
Try this:

(abc[a-zA-Z]*?){3}(?!.*abc)

tested at regexpal.com
0
 
LVL 14

Assisted Solution

by:sentner
sentner earned 75 total points
ID: 34124381
Can you use 2 separate regex's?  For example, this pseudocode:

if /(abc.*){3}/ then
   if /(abc.*){4}/ return false     # More than 3 matches
   else return true                      # Exactly 3 matches
else return false                        # Less than 3 matches


0
 
LVL 82

Author Comment

by:leakim971
ID: 34124429
Why a regex ? I can use it as a parameter for example.
@phr0ze and @sentner thank you I'm going to try your propositions.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34124637
@phr0ze

The problem with your pattern is that the *? on the bracket expression will consume any trailing "abc" strings after the first three. Simply adding the negative lookahead does not overcome this.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34124901
I believe the following satisfies the requirement:
^(?=(?:(?:[^a]|a[^ab])*abc){3}(?!.*?abc)).*$

Open in new window

0
 
LVL 82

Author Comment

by:leakim971
ID: 34124980
kaufmed this one don't match : affkaabcsjfjabckfsofabc
affk a abc sjfj abc kfsof abc

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34124988
It seems it did not upon further testing. Revised:
^(?=(?:(?:[^a]|a[^b]|a(?=a))*abc){3}(?!.*?abc)).*$

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34124994
Yeah, the double-a killed it. The modified version has this corrected.
0
 
LVL 82

Author Comment

by:leakim971
ID: 34125007
Don't match with : abffkabcsjfjabckfsofabc

ab ffk abc sjfj abc kfsof abc
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 375 total points
ID: 34125071
Third time's a charm?

:)
^(?=(?:(?:a(?=a)|a[^b]|ab[^c]|[^a])*abc){3}(?!.*?abc)).*$

Open in new window

0
 
LVL 82

Author Comment

by:leakim971
ID: 34125126
@kaufmed, yes it work this time, thanks. The "problem" with this regexp is the way we build it dynamically.
The original sequence need to have three characters for example : abc

Anyway I want to thank you a lot for your time. I'm going to use an array of regex+boolean and use the logic of @sentner

I'm going to close the question
Thank you to everyone for your suggestions, have a great weekend!
0
 
LVL 82

Author Closing Comment

by:leakim971
ID: 34125198
End of week, enjoy, have a nice weekend!
Thanks a lot to everyone! Have fun in your life!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34125279
I figured I'd throw this out there just in case, but the string shouldn't be that difficult to build dynamically; granted I don't know what your environment is like. Here is a function I created to build the pattern string:
function buildPattern(src)
{
	var basePattern = "^(?=(?:(?:@@@)*###){3}(?!.*?###)).*$";
	var temp = "";
	var t = src.charAt(0);
	
	for (i = 1; i < src.length; i++)
	{
		temp += "|" + t + "[^" + src.charAt(i) + "]";
		t += src.charAt(i);
	}

	temp = src.charAt(0) + "(?=" + src.charAt(0) + ")" + temp;
	
	return basePattern.replace(/###/g, src).replace(/@@@/, temp);
}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34125286
Of course, make sure your source strings don't contain the sequences "###" or "@@@"   :)
0
 
LVL 82

Author Comment

by:leakim971
ID: 34125312
Thanks a lot @kaufmed!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34125317
Sure. Glad to help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now