?
Solved

Ensuring an * only exists at beginning or end of string

Posted on 2009-05-08
6
Medium Priority
?
215 Views
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...


0
Comment
Question by:Prysson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

by:Acumentality
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

0
 
LVL 1

Accepted Solution

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

Open in new window

0
 
LVL 16

Expert Comment

by:ToddBeaulieu
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.");
else
    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;
				break;
			}
			
			i = search.IndexOf('*', i+1);
		}
 
		if (failed)
			Console.WriteLine("String is bad.");
		else
			Console.WriteLine("String is good.");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Acumentality
ID: 24337905
For 0 or 1:
^[\*]?[^\*]*[\*]?

Open in new window

0
 
LVL 1

Expert Comment

by:Acumentality
ID: 24337916
Actually...
^[\*]?[^\*]*[\*]?$

Open in new window

0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24337919
Acumentality: of course! :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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