469,329 Members | 1,373 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

regexp help on part match

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
Aug 27 '08 #1
4 1174
Andrew Poulos <ap*****@hotmail.comwrites:
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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 27 '08 #2
Lasse Reichstein Nielsen wrote:
Andrew Poulos <ap*****@hotmail.comwrites:
>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?
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
Aug 27 '08 #3
pr
Lasse Reichstein Nielsen wrote:
Andrew Poulos <ap*****@hotmail.comwrites:
>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;
Alternatively:

var parts = string.split(".");
var isValid = parts.length == 4 && parts[0] == "cmi" &&
parts[1] == "interactions" && parts[2] != "" && parts[3] == "id";
Aug 27 '08 #4
Andrew Poulos <ap*****@hotmail.comwrites:
Lasse Reichstein Nielsen wrote:
>First, why use a regexp?

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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by python_charmer2000 | last post: by
1 post views Thread by joh12005 | last post: by
20 posts views Thread by RobG | last post: by
reply views Thread by Chris Croughton | last post: by
3 posts views Thread by jasonkester | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
11 posts views Thread by HopfZ | last post: by
6 posts views Thread by runsun pan | last post: by
27 posts views Thread by SQL Learner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.