aspose file tools*
The moose likes Java in General and the fly likes How to regex made up of numbers surrounded by alphabets & special chars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to regex made up of numbers surrounded by alphabets & special chars" Watch "How to regex made up of numbers surrounded by alphabets & special chars" New topic
Author

How to regex made up of numbers surrounded by alphabets & special chars

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

I need your advice on coming up with a regex that matches the following list of numbers potentially surrounded by a combination of alphabets and special characters:



Can someone confirm whether the last regex is correct? Otherwise, what would the correct regex be?

This is the address number of a public property sales report.

Thanks a lot,

Jack
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

haven't really looked at your examples, but I have to ask one question...

Is it a requirement that you find all the possible matches in one pass? Might it be easier to have several regexes, each of which match a (possibly distinct) subset of what you want, then you combine them later?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
This may work. Please try.


Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Thanks to both Fred & Harsha for your suggestions.

Yes, it is a requirement to identify that it is a property number from its sequence (first number) as well as ensuring that it consists of at least one digit. Below is an example of what this input data is made up of:


Any idea on how this could be achieved?

Thanks again,

Jack
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jack Bush wrote:Yes, it is a requirement to identify that it is a property number from its sequence (first number) as well as ensuring that it consists of at least one digit. Below is an example of what this input data is made up of:The first 2 words is the district name which can start from 1 – 3 letters or more...

Actually, it isn't. From what I can see from your original data (the one you posted in your last thread), the district ends with the first word
that contains a number as it's first character (which seems, pretty consistently, to be the start of an address).

That word seems to contain some combination of the following:
1. A house/building number, which may be suffixed with a letter (eg, '43a').
2. A range of building numbers, separated by a hyphen (-).
3. A suite or apartment number + plus a building number (or range), separated by a forward slash (/).

I reckon Fred's right. Once you've identified the first word of the address, I think you might be better off breaking down the possibilities,
maybe with String.split(), and then using individual regexes to validate/extract the actual numbers.

BTW, as far as I can see, the document is also consistent about having all that "numeric stuff" in a single word (ie, no spaces),
so if you don't need to actually parse the contents, you could simply use "[0-9][^ ]*" to get the whole word.

Winston
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
I need a regex that make digits mandatory while the rest are optional.



This the regex


nick kaushik
Ranch Hand

Joined: Sep 25, 2009
Posts: 48

& while we are at it could you guys also tell me regex for fishing out strings from text such that it doesn't contain any space,newline,tabs,etc & the full stops.....
i know split method fish out line feeds & whitespace with "//s" but what about the dot.
eg.
i am doing Java.
output should be:
i
am
doing
Java


"ye shall know the truth & the truth shall set you free..."
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

Apologies for the late reply but I have applied Fred & Winston’s suggestion of splitting each word out and match them with their respective regexes in sequences, which is working out finally. I am still at an experimental stage but it is looking good so far.

Also thank you to Hersha for kept coming up with great regex gems.

I will close this thread now since the question has been answered.

Thank you again,
Jack
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to regex made up of numbers surrounded by alphabets & special chars