HighDots Forums  

regexp help on part match

Javascript JavaScript language (comp.lang.javascript)


Discuss regexp help on part match in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andrew Poulos
 
Posts: n/a

Default regexp help on part match - 08-27-2008 , 12:56 AM






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

Reply With Quote
  #2  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: regexp help on part match - 08-27-2008 , 01:13 AM






Andrew Poulos <ap_prog (AT) hotmail (DOT) com> writes:

Quote:
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.'


Reply With Quote
  #3  
Old   
Andrew Poulos
 
Posts: n/a

Default Re: regexp help on part match - 08-27-2008 , 01:24 AM



Lasse Reichstein Nielsen wrote:
Quote:
Andrew Poulos <ap_prog (AT) hotmail (DOT) com> writes:

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.

Quote:
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


Reply With Quote
  #4  
Old   
pr
 
Posts: n/a

Default Re: regexp help on part match - 08-27-2008 , 08:00 AM



Lasse Reichstein Nielsen wrote:
Quote:
Andrew Poulos <ap_prog (AT) hotmail (DOT) com> writes:

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";


Reply With Quote
  #5  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: regexp help on part match - 08-27-2008 , 11:37 AM



Andrew Poulos <ap_prog (AT) hotmail (DOT) com> writes:

Quote:
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.'


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.