Regex help

  • Thread starter Thread starter Epetruk
  • Start date Start date
E

Epetruk

Hello,

I'm trying to write a regular expression that extracts the host name from a
string.

In other words, the string I extract must have:

- only letters, numbers and fullstops in it;

- the fullstops most be in the middle of it.


So if I have a string like this:

"this is a host.domain.com name that I want"

what kind of regular expression would I write to extract 'host.domain.name'?
 
Hello,

I'm trying to write a regular expression that extracts the host name from a
string.

In other words, the string I extract must have:

- only letters, numbers and fullstops in it;

- the fullstops most be in the middle of it.

So if I have a string like this:

"this is a host.domain.com name that I want"

what kind of regular expression would I write to extract 'host.domain.name'?

First take the input string and remove all other characters except
letters, numbers, and fullstops using
then trim any fullstops on each side

something like the one below ( I have not tested it ... :) )
string hostname = Regex.Replace(str, @"[^ .a-zA-Z0-9]",
"").Trim(".".ToCHarArray());

Let me know if its of any help to you !!
 
Hello Epetruk,
Hello,

I'm trying to write a regular expression that extracts the host name
from a string.

In other words, the string I extract must have:

- only letters, numbers and fullstops in it;

- the fullstops most be in the middle of it.

So if I have a string like this:

"this is a host.domain.com name that I want"

what kind of regular expression would I write to extract
'host.domain.name'?

\b(?i:[0-9a-z]+(\.[0-9a-z]+)+)\b

should do the trick.
 

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


Back
Top