This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
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

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "regular expression" Watch "regular expression" New topic

regular expression

Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Posts: 41

My test input is like this<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

Joined: May 26, 2003
Posts: 32328

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.

[OCA 8 book] [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 = " :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 = "";

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:


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 = " iskarrays-sn-a8675309";
This matches well.

I could be wrong. But the reason it matches is that it starts with "" 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.
Consider Paul's rocket mass heater.
subject: regular expression
It's not a secret anymore!