Regular Expressions

K

Kristian

I have a program which recives a string with an address.
The string has no spesific format and I would like to extract the entrance
character.

some rules for the regular expression: one char, standing alone, can or
canot contain space


string eksample: "flowstreet 2 B"

here i would like to extract the B.

I ve tried
Regex myRegex = new Regex([^a-zA-Z0-9][a-zA-Z]{1}[^a-zA-Z0-9],
RegexOptions.IgnoreCase);

it works if the strign contains a space at the end
string eksample: "flowstreet 2 B " but not when it comes, "flowstreet 2 B" ,
without the space

Anyone who knows whats missing in the regular expression statement ?

Regards
Kriss.
 
O

Oliver Sturm

Kristian said:
some rules for the regular expression: one char, standing alone, can or
canot contain space


string eksample: "flowstreet 2 B"

here i would like to extract the B.

I ve tried
Regex myRegex = new Regex([^a-zA-Z0-9][a-zA-Z]{1}[^a-zA-Z0-9],
RegexOptions.IgnoreCase);

it works if the strign contains a space at the end
string eksample: "flowstreet 2 B " but not when it comes, "flowstreet 2 B" ,
without the space

Sorry, I don't understand the expression you are mentioning yourself,
because it doesn't seem to match your string at all. I'll try to make
another suggestion, though.

From your example I would suspect that the character in question is
always the last non-whitespace character in the line. If that's actually
the case, you'll do fine with

(?<entrance>[^\s])\s*$

This expression extracts the character itself in the group named
"entrance", without any whitespace that may be following.

If you want to check the line's format at the same time, and you are not
sure whether the character in question is going to be there at all, this
expression should check the complete line:

(?<street>.+)\s+(?<number>\d+)(\s+(?<entrance>\w))?\s*$

This will extract the street, the house number and the entrance
character, if one is available. Works even if the street has spaces :)

Hope this helps!


Oliver Sturm
 
K

Kristian

Thanks for your answer.

I ll try to explain better. The problem is that the strings i receiv has no
specific format for the entrance.
I been able to cut the strings so only entranse remain.
Entrance can be one of the followin (+ more)
"B"
" B"
"B"
" B "
"B,"
",B"
",B "

and i want do extract only one character (the entrance character).

I am thankfull for any advice that brings me closer to the solution..

Regards
Kriss.


Oliver Sturm said:
Kristian said:
some rules for the regular expression: one char, standing alone, can or
canot contain space


string eksample: "flowstreet 2 B"

here i would like to extract the B.

I ve tried
Regex myRegex = new Regex([^a-zA-Z0-9][a-zA-Z]{1}[^a-zA-Z0-9],
RegexOptions.IgnoreCase);

it works if the strign contains a space at the end
string eksample: "flowstreet 2 B " but not when it comes, "flowstreet 2
B" , without the space

Sorry, I don't understand the expression you are mentioning yourself,
because it doesn't seem to match your string at all. I'll try to make
another suggestion, though.

From your example I would suspect that the character in question is always
the last non-whitespace character in the line. If that's actually the
case, you'll do fine with

(?<entrance>[^\s])\s*$

This expression extracts the character itself in the group named
"entrance", without any whitespace that may be following.

If you want to check the line's format at the same time, and you are not
sure whether the character in question is going to be there at all, this
expression should check the complete line:

(?<street>.+)\s+(?<number>\d+)(\s+(?<entrance>\w))?\s*$

This will extract the street, the house number and the entrance character,
if one is available. Works even if the street has spaces :)

Hope this helps!


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
O

Oliver Sturm

Kristian said:
I ll try to explain better. The problem is that the strings i receiv has no
specific format for the entrance.
I been able to cut the strings so only entranse remain.
Entrance can be one of the followin (+ more)
"B"
" B"
"B"
" B "
"B,"
",B"
",B "

and i want do extract only one character (the entrance character).

Well, have you actually tried my solutions? You should be able to get
most of your examples working with them already, and the others are
really just minor modifications.

For example, I changed the second of my two expressions to this:

(?<street>.+)\s+(?<number>\d+)(,?\s*(?<entrance>\w))?\s*$

(compare it carefully to the first version, there are subtle
differences), and this covers all the following address strings
correctly (in quotes to show the spaces):

"flowstreet 2 B"
"flowstreet 2 B "
"flowstreet 2"
"flow street 35 a"
"flowstreet 2,B "
"flowstreet 2, B "

I advise you to get hold of Regulator from http://regex.osherove.com/
and start playing around with these expressions. To be able to fine tune
(and maintain, even more importantly!) these expressions later on, you
should really understand them in depth, not just copy them from a
newsgroup or a web site.


Oliver Sturm
 
K

Kristian

Oliver Sturm said:
Well, have you actually tried my solutions? You should be able to get most
of your examples working with them already, and the others are really just
minor modifications.

For example, I changed the second of my two expressions to this:

(?<street>.+)\s+(?<number>\d+)(,?\s*(?<entrance>\w))?\s*$

(compare it carefully to the first version, there are subtle differences),
and this covers all the following address strings correctly (in quotes to
show the spaces):

"flowstreet 2 B"
"flowstreet 2 B "
"flowstreet 2"
"flow street 35 a"
"flowstreet 2,B "
"flowstreet 2, B "

I advise you to get hold of Regulator from http://regex.osherove.com/ and
start playing around with these expressions. To be able to fine tune (and
maintain, even more importantly!) these expressions later on, you should
really understand them in depth, not just copy them from a newsgroup or a
web site.


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Hi Oliver.

Thank you very much for taking the time to write a good answer. It solved my
problem.
I used the last part .... (,?\s*(?<entrance>\w))?\s*$
I ll do some testing and check out the website you recommended.

Best Regards,
Kriss.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top