• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression

 
Parashuram Hallur
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My test input is like this

iqn.2001-01.com.mydomain.domain:<alphanumerictext>:<alphanumerictext>
(iqn. <date> . <domain> : <some identifier>

Input should always start with the "iqn" and the date format should be in yyyy-mm and then domain followed by optional ':' then any alphanumeric characters.

I want the regular expression for this. I have tried my best could not come out with the correct one . Any help is much appreciated.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34651
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parashuram,
What do you have so far? The trick to building a regular expression is to do it in manageable parts and then combine them.

Can you match:
- a string starting with iqn?
- a dot
- a string in the format yyyy-mm
- a domain
- an optional character
- any alphanumeric text

Try posting the regular expression for the ones you have. And what you have tried for the ones you are stuck on.
 
Parashuram Hallur
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne for the response.

I'm newbie at writing regular expression.
Here is what I tried

regExp = /^(iqn|eui)\.(\d){4}\-(\d){2}\.[a-zA-Z]{2,3}\.[\w\-\.](\:?)[\w\-\:\.]/;


I have tried to combine everything in one expression.
^(iqn) - string starting with the iqn or eui
. - the compulsary dot
(\d){4} - for yyyy
(\d){2} - for mm
[a-zA-Z]{2,3} - for com, org, net etc and also to allow 2 chars if the domain ends with any of the country codes
[\w\-\.] - for the other part of the domain
(\:?) - optional colon
[\w\-\:\.] - alphanumeric string, and for the dot, colon and hyphen. The string after the colon can also contain the hyphen, dot and colon

input tried
input = "iqn.2001-02.com.example.cn.sdns:storage :diskarrays-sn-a8675309";
This matches well.

Some times I suspect that it could fail for the date format. Say if input is like below
input = "iqn.sdsd2001-sds02.com.example.cn.sdns:storage:diskarrays-sn-a8675309";

Is the reg exp I wrote is correct, does it not break for the date validation. Please let me know

Thanks in advance

[ August 07, 2007: Message edited by: Parashuram Hallur ]
[ August 07, 2007: Message edited by: Parashuram Hallur ]
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been a while since I last used RegExp but this part of your expression:

[\w\-\.]


should match a single alphanumeric character or a single '-' or a single '.'. But it seems that you want to match a string of alphanumeric character.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

input tried
input = "iqn.2001-02.com.example.cn.sdns:storage iskarrays-sn-a8675309";
This matches well.

I could be wrong. But the reason it matches is that it starts with "iqn.2001-02.com.ex" which matches your regexp. Consider
using [\w\-\.]+ and changing (\:?) to [\:]. That is, one or more alphanumeric char, '-' or '.' and a compulsory single ':' .
 
Parashuram Hallur
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thansk Alec, yes the + should be added to allow the multiple occurrence.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic