This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check for specific string pattern(using jdk 131)

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I need to check for different ways 'P O BOX' appears in a string how can i do that( i need to see if P appears before O and O appears before Box)etc.

Can I do that easily in jdk 131??
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can achieve this in JDK 1.3.1 using charAt(int) and looping through your string.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nigel Browne:
You can achieve this in JDK 1.3.1 using charAt(int) and looping through your string.


Instead of charat, I can do a index of for P,O and BOX and see if there is a index and the index fall in that order.

My question is, is there a pattern matching function in java.util which can do this in one single api call???
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pattern matching got added to java in the 1.4 version specifically in the java.util.regex package
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kishore Dandu:



My question is, is there a pattern matching function in java.util which can do this in one single api call???



yeah a single api can get you the result if you use jdk 1.4 (java.uti.regex)

You may try this..

 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also copy the string to a string buffer and delete all the non-alpha characters then check for POBox using indexOf. Without regex, there's no simple way to do it. Another possibility is to construct an array of strings with each element containing one possible permutation of "PO Box", then use indexOf from either String or StringBuffer to see if it's in the string. An advantage to this is that you could use a parallel array to hold the index of the occurrance, so you could tell which usage exists and where it is in the original test string.

Are you constrained to 1.3 because of project or customer requirements? If not, you may want to upgrade and use regex.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if constrained by the jdk version, you might alo want to consider jakarta.apache.org's ORO and regexp libraries.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am constrained to 1.3.1 for the time being due to technology choices at work.

Here is the solution I am using.

1)Check for index of p, if it is there check for index of o
2)if o index is more than that of p; check for index of box
3) if box index is morethan index of o index(it happens only in case there are two os in the string) then there is PO BOX in the string.

What restricts me is I am filter some address if it is 'Point box road' since it has 'po box' in it
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Kishore -

I think my String array method would eliminate the error you're seeing. Can't hurt to try.

Good luck!

Cheers,

Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic