aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java regex issue. Thank you in advance! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java regex issue. Thank you in advance!" Watch "Java regex issue. Thank you in advance!" New topic
Author

Java regex issue. Thank you in advance!

Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Dear,

I have to use the Java regex to check whether a String conform to the below format:

1. Fix length: 10;
2. Start with a word which is composed of non-space characters (\S);
3. If the word is shorter than 15, filled with white spaces (\s) to 15;

For example,
"ABCDEFGHI " is conform to this format.
"ABC DEFGHI " is not.

Could anybody help me work out a regular expression to check it?

Thank you very much in advance!

Larry

[Bear edit: removed "urget" from title]
[ April 24, 2006: Message edited by: Bear Bibeault ]

The hill is not a hill.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
I have to use the Java regex

Why? Is this some kind of assignment that you should be figuring out by yourself? What do you have so far, and where are you stuck?


Ping & DNS - updated with new look and Ping home screen widget
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

It has to be a maximum of ten characters, and include enough whitespace padding to make it 15 characters? That's unpossible!


[Jess in Action][AskingGoodQuestions]
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Thanks for your reply!

The requirement is to check if each line of a text file follows some specific format or not.

For example,
A line of that file requires the first field contains 10 characters, and the second field contains 5, and the third field contains 5. A valid line should look like this: (# for spaces)
field1####f2###f3###

I hope to use a single regex pattern to represent this format, so to simplify the check.



It has to be a maximum of ten characters, and include enough whitespace padding to make it 15 characters? That's unpossible!


Sorry that this was a mistake. It should be a maximum of 10 characters, and if the string (no space in it) is less than 10, padding it to 10 characters, not 15.
[ April 24, 2006: Message edited by: Larry Li ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

In that example, any string of 18 characters will satisfy the stated rules. You don't need a regex to just check that a string is 18 characters long.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

A line of that file requires the first field contains 10 characters, and the second field contains 5, and the third field contains 5. A valid line should look like this: (# for spaces)
field1####f2###f3###

I hope to use a single regex pattern to represent this format, so to simplify the check.


First, my standard soapbox comment... You really should get very comfortable with regular expressions. It will make your parsing life much easier in many many cases... off the soapbox.

To answer your question... To match the above line (with the three fields), you can use "^\\S{1,10}\\s+\\S{1,5}\\s+\\S{1,5}\\s*$" as the regular expression.

Henry
[ April 24, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
To answer your question... To match the above line (with the three fields), you can use "^\\S{1,10}\\s+\\S{1,5}\\s+\\S{1,5}\\s*$" as the regular expression.


I don't think this pattern could work, "abc##abc##abc#" for instance, matches this pattern, but does not follow the format, which requires the first field fed with 7 spaces.

Thanks,

Larry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by Larry Li:

I don't think this pattern could work, "abc##abc##abc#" for instance, matches this pattern, but does not follow the format, which requires the first field fed with 7 spaces.

Thanks,

Larry


I am going to stick my neck out and state that this request is not possible with a single regular expression. Hopefully, someone will prove me wrong on this...

Anyway, to do this with many regular expressions...



Henry
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Hi Henry,

I've been thinking of this for days and finally used the similar solution as you provided. I agree with you that this is not likely done in one single expression.

Thank you very much anyway.

Larry
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Regex questions belong in the Java in General: Intermediate forum. You're more likely to get useful answers if you post them there (unfortunately, there is no better answer to this one).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
Originally posted by Alan Moore:
Regex questions belong in the Java in General: Intermediate forum.


This kind of question could go into one of a number of forums. Right here if it is about the use of java.util.regex, "General Computing" if it is about regexps in general, or one of the Java in General forums if it is about using regexp in Java. (But the moderators will move it to wherever they feel like anyway, so who knows for sure )
[ April 28, 2006: Message edited by: Ulf Dittmer ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
The moderators used to move all regex questions to the Intermediate forum, so that's where I expect to find them. There's no obviously correct place for regex questions, but the Java in General forums seem like the best place to me (if only because I'm too lazy to check all the forums).

My understanding is that this forum is for third-party API's and optional packages like JavaHelp and JavaMail. So if you're using Jakarta ORO, and you have a question about using its API and not how to write the regex, this would seem to be the right place to ask it.

Of course, the best place would be a dedicated regex forum...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
Not quite. This forum is for Sun APIs only, so Jakarta questions would definitely be moved to "Other Open Source Projects", while questions about commercial packages would go to "Other Java Applications and Servers".

None of these rules is hard and fast, though. E.g. JavaMail questions should go into "EJB and Other J2EE technologies" because JavaMail is an J2EE API, but we're keeping them here anyway.

Of course, being the moderator of this forum, I like to enforce some rules that exist solely in my head, just to mess with peoples minds
[ April 28, 2006: Message edited by: Ulf Dittmer ]
 
 
subject: Java regex issue. Thank you in advance!
 
Similar Threads
Regular Expression in String
Regular Expression in String
how can i separate string?
Could you please explain me the meaning of this RegEx?
How to write a regex to include a text but exclude another text in the one regex?