• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex Expression.  RSS feed

 
mahadev ramalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking for regular expression, which matches two conditions.
1) Should start with a prefix
2) should not start with a suffix.

First one, I could achieve with ^prefix_
second one, I'm not able to get in the same expression.
I tried this:

(^prefix_).[^\.suffix]

But the second half of it doesn't work.
the match gives us the results even if the input string has .suffix at the end.

Can anyone help in this regard?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahadev,

You might want to consider getting a good book, or going through Sun's tutorial on Regular Expressions. As it looks like your first attempt was a guess.

In any case, could you give us a few examples? A few cases where you want the regex to match. And a few that you don't want the regex to match. Not sure what you meant by "must start with prefix, BUT not start with suffix". Doesn't starting with a prefix automatically imply that it is not starting with the suffix?

Henry
 
mahadev ramalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply...

I got the solution.
I used this :
(^prefix)[A-Za-z0-9]*(?!suffix)
 
mahadev ramalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry,

Sorry if I confused you.

May be I didn't put in the question correct.

Example: filename.ext
here, prefix is file and suffix is .ext
I needed my adapter to match and pick all the files with names starting with the prefix(in this case 'file') but it should not pick the files with name ending with suffix. (in this case, .ext).

If I'm still confusing you, sorry...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is in fact a large difference between "should not start with a suffix" (what you said originally) and "should not pick the files with name ending with suffix" (what you just said). start != end. Looks like you've got a solution though, so I guess that does it.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got the solution.
I used this :
(^prefix)[A-Za-z0-9]*(?!suffix)

If I'm still confusing you, sorry...


Okay, so you meant "start with prefix", but "not end with suffix". Got it. Having said that... I don't see how this regex would work.

Assuming that you are using the find() method, then if the file does start with the correct prefix, it should always match, as the regex will simply back-off (be not as greedy in the middle) as necessary, to allow the negative look-ahead to work.

Assuming that you are using the matches() method, and the files does start with the correct prefix, then I don't see it matching at all as you never tried to match the ".". And if the "." is in the negative look-ahead, then it should always fail as you will have stuff left over as the look-ahead is always zero-length.

For my curiousity, I would really like to see which regex method you are using and/or how you are using it -- as the basic use of the two main ones (matches() and find()) shouldn't work for the suffix.

Thanks,
Henry
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW... Here is how I would do it via regex. (I added the possessive qualifier to prevent back-off, and the end tag so that suffixes like ".external" would be okay.)



Personally, I think this may all be overkill... what is wrong with just doing this?



Henry
[ December 30, 2006: Message edited by: Henry Wong ]
 
mahadev ramalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim,

I totally agree. I did realize the mistake after you pointed out the mistake. ("start != end")
 
mahadev ramalingam
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry,

Yes. there is a . in the expression.
Exact phrase is : (^ABC)[A-Za-z0-9]*(?!.)

I didn't add the $ at the end, because, for my specific requirement, any extension wasn't required.

I had to use regex, as the adapter picks up based on a configuration, which has to be regex.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This regex should work the same whether you use matches() or find().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!