aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "regular expression" Watch "regular expression" New topic
Author

regular expression

Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Posts: 41
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

Joined: May 26, 2003
Posts: 30913
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Posts: 41
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

Joined: Jan 28, 2004
Posts: 569
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

Joined: Jan 28, 2004
Posts: 569

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

Joined: Sep 18, 2006
Posts: 41
Thansk Alec, yes the + should be added to allow the multiple occurrence.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression