Ensuring an * only exists at beginning or end of string

Posted on 2009-05-08
Last Modified: 2012-05-06
I need to verify that a string only contains * at the beginning r end of the string.

Conatining * in teh middle o fthe string is not ok.  
so *dsjdh   would be fine
*ddd* woudl be fine
whx* woudl be fine
but g*g would not be fine..

Anyone have anyidea how I can check this...

Question by:Prysson
  • 4
  • 2

Expert Comment

ID: 24337693

string s = "*aste*risk*";

bool inside = s.SubString(1, s.length-2).Contains("*");

//*asterisk is false, asterisk* is false, aste*risk is true, *aste*risk* is true

Open in new window


Accepted Solution

Acumentality earned 500 total points
ID: 24337724
Sorry, some typos.
bool inside = s.Substring(1, s.Length-2).Contains("*");

Open in new window

LVL 16

Expert Comment

ID: 24337752
I'd probably just compare the results of a regular expression against the input string and if they match, it's good. The following example searches for an expression that allows 0 or more "*"'s the start of the string, 0 or more characters in the string, as long as they aren't "*"'s and then 0 or more "*"'s at the end.

This seems to work correctly, except as I typed this it was obvious the "or more" isn't what you want for the "*"s. There is an "exactly 1", but I assume you want to allow "zero" leading/trailing wildcards.

See the second block in the code snippet for a brute force approach.
using System.Text.RegularExpressions;


string pattern = @"^[\*]*[^\*]*[\*]*";

string search = @"*this is a * good example";

Regex regEx = new Regex(pattern);

Match match = regEx.Match(search);

if (match.Value == search)

    Console.WriteLine("Input string is good.");


    Console.WriteLine("Input string is bad.");

		string search = @"*this is* search string*";


		int i = search.IndexOf('*');

		int c = search.Length;

		bool failed = false;


		while(i < c && i != -1)


			Console.WriteLine("Found a wildcard at position {0} in a string of {1} characters.", i, c);


			if (i != 0 && i != c-1)


				failed = true;




			i = search.IndexOf('*', i+1);


		if (failed)

			Console.WriteLine("String is bad.");


			Console.WriteLine("String is good.");

Open in new window

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Expert Comment

ID: 24337905
For 0 or 1:

Open in new window


Expert Comment

ID: 24337916

Open in new window

LVL 16

Expert Comment

ID: 24337919
Acumentality: of course! :)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 35
How to open/READ file from UNC path Shared Folder... 2 40
Expando 4 36
Iterate two lists and remove inactive 11 38
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now