• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may work. Please try.


 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a regex that make digits mandatory while the rest are optional.



This the regex


 
nick kaushik
Ranch Hand
Posts: 48
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& 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
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic