Connecting Tech Pros Worldwide Help | Site Map

regexp help on part match

  #1  
Old August 27th, 2008, 06:05 AM
Andrew Poulos
Guest
 
Posts: n/a
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
  #2  
Old August 27th, 2008, 06:15 AM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: regexp help on part match


Andrew Poulos <ap_prog@hotmail.comwrites:
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.'
  #3  
Old August 27th, 2008, 06:25 AM
Andrew Poulos
Guest
 
Posts: n/a

re: regexp help on part match


Lasse Reichstein Nielsen wrote:
Quote:
Andrew Poulos <ap_prog@hotmail.comwrites:
>
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?
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
  #4  
Old August 27th, 2008, 01:05 PM
pr
Guest
 
Posts: n/a

re: regexp help on part match


Lasse Reichstein Nielsen wrote:
Quote:
Andrew Poulos <ap_prog@hotmail.comwrites:
>
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;
Alternatively:

var parts = string.split(".");
var isValid = parts.length == 4 && parts[0] == "cmi" &&
parts[1] == "interactions" && parts[2] != "" && parts[3] == "id";
  #5  
Old August 27th, 2008, 04:45 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: regexp help on part match


Andrew Poulos <ap_prog@hotmail.comwrites:
Quote:
Lasse Reichstein Nielsen wrote:
Quote:
>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.'
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some help with replace() Roy W. Andersen answers 3 June 17th, 2006 11:05 PM
REQ: Question on how to call functions (New JavaScript Student) Sue answers 5 July 20th, 2005 01:49 PM
multi regexp analyzer ? or how to do... joh12005@yahoo.fr answers 1 July 19th, 2005 03:30 AM
How do I parse this ? regexp ? serpent17@gmail.com answers 7 July 19th, 2005 01:44 AM