• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java regex issue. Thank you in advance!

 
Larry Li
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to be a maximum of ten characters, and include enough whitespace padding to make it 15 characters? That's unpossible!
 
Larry Li
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20206
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Larry Li
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic