regexp help on part match

A

Andrew Poulos

I have a string that looks like this

"cmi.interactions.fred.id"

I need to test that:
- the first part of the string is "cmi.interactions." (case sensitive).
- the last part of the string is ".id" (case sensitive).
- that there's 1 or more characters between the 2nd and third dots (in
the above example its "fred")
- that there's only 3 dots in the whole string

Could someone please write a regexp for me. I've had a few goes but it
keeps failing.

Andrew Poulos
 
L

Lasse Reichstein Nielsen

Andrew Poulos said:
I have a string that looks like this

"cmi.interactions.fred.id"

I need to test that:
- the first part of the string is "cmi.interactions." (case sensitive).
- the last part of the string is ".id" (case sensitive).
- that there's 1 or more characters between the 2nd and third dots (in
the above example its "fred")
- that there's only 3 dots in the whole string

Could someone please write a regexp for me. I've had a few goes but it
keeps failing.

First, why use a regexp?

var valid =
string.length > 20 &&
string.substring(0,17) == "cmi.interactions." &&
string.substring(string.length-3) == ".id" &&
string.indexOf(".",17) == string.length - 3;

But if you insist on a regexp:
/^cmi\.interactions\.[^.]+\.id$/

/L
 
A

Andrew Poulos

Lasse said:
First, why use a regexp?

There's about a dozen items that start with "cmi.interactions" bu have
different endings.
var valid =
string.length > 20 &&
string.substring(0,17) == "cmi.interactions." &&
string.substring(string.length-3) == ".id" &&
string.indexOf(".",17) == string.length - 3;

But if you insist on a regexp:
/^cmi\.interactions\.[^.]+\.id$/

/L

Thank you.

Andrew Poulos
 
P

pr

Lasse said:
First, why use a regexp?

var valid =
string.length > 20 &&
string.substring(0,17) == "cmi.interactions." &&
string.substring(string.length-3) == ".id" &&
string.indexOf(".",17) == string.length - 3;

Alternatively:

var parts = string.split(".");
var isValid = parts.length == 4 && parts[0] == "cmi" &&
parts[1] == "interactions" && parts[2] != "" && parts[3] == "id";
 
L

Lasse Reichstein Nielsen

Andrew Poulos said:
There's about a dozen items that start with "cmi.interactions" bu have
different endings.

You'll need different regexps for those too.
But it's not a problem to generalize:

function isValid(string, ending) {
var el = ending.length;
var sl = string.length;
return string.length > 18+el &&
string.substring(0,17) == "cmi.interactions." &&
string.indexOf(".",17) == sl-el-1 &&
string.substring(sl-el) == ending;
}


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,812
Members
47,357
Latest member
sitele8746

Latest Threads

Top