Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:58:29+00:00 2026-05-24T20:58:29+00:00

I was wondering if someone out there could help me with a regex in

  • 0

I was wondering if someone out there could help me with a regex in C#. I think it’s fairly simple but I’ve been wracking my brain over it and not quite sure why I’m having such a hard time. 🙂

I’ve found a few examples around but I can’t seem to manipulate them to do what I need.

I just need to match ANY alphanumeric+dashes subdomain string that is not “www”, and just up to the “.”

Also, ideally, if someone were to type “www.subdomain.domain.com” I would like the www to be ignored if possible. If not, it’s not a huge issue.

In other words, I would like to match:

  • (test).domain.com
  • (test2).domain.com
  • (wwwasdf).domain.com
  • (asdfwww).domain.com
  • (w).domain.com
  • (wwwwww).domain.com
  • (asfd-12345-www-bananas).domain.com
  • www.(subdomain).domain.com

And I don’t want to match:

  • (www).domain.com

It seems to me like it should be easy, but I’m having troubles with the “not match” part.

For what it’s worth, this is for use in the IIS 7 URL Rewrite Module, to rewrite for all non-www subdomains.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:58:30+00:00Added an answer on May 24, 2026 at 8:58 pm

    Is the remainder of the domain name constant, like .domain.com, as in your examples? Try this:

    \b(?!www\.)(\w+(?:-\w+)*)(?=\.domain\.com\b)
    

    Explanation:

    • \w+(?:-\w+)* matches a generic domain-name component as you described (but a little more rigorously).

    • (?=\.domain\.com\b) makes sure it’s the first subdomain (i.e., the last one before the actual domain name).

    • \b(?!www\.) makes sure it isn’t www. (without the \b, it could skip over the first w and match just the ww.).

    In my tests, this regex matches precisely the parts you highlighted in your examples, and does not match the www. in either of the last two examples.


    EDIT: Here’s another version which matches the whole name, capturing the pieces in different groups:

    ^((?:\w+(?:-\w+)*\.)*)((?!www\.)\w+(?:-\w+)*)(\.domain\.com)$
    

    In most cases, group $1 will contain an empty string because there’s nothing before the subdomain name, but here’s how it breaks down www.subdomain.domain.com:

    $1: "www."
    $2: "subdomain"
    $3: ".domain.com"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

was wondering if anyone could help me out. I have been looking all over
Just wondering if someone could help me with a very simple SQL query. I
Im wondering if someone could help me out with a little issue im having.
was wondering if someone out there could tell me how to display a variable
I was wondering if someone out there could give me a pointer to reducing
I was wondering if someone can help me out - working on a bit
I was wondering if someone can help me out with this issue. I have
Wondering if someone could help me. I have next to no knowledge with Ajax,
I was wondering if someone could explain to me how to make a simple
I'm wondering if someone could look over my code. I'm trying to pass a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.