Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Ensuring an * only exists at beginning or end of string

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
Prysson
Asked:
Prysson
  • 4
  • 2
1 Solution
 
AcumentalityCommented:

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
 
AcumentalityCommented:
Sorry, some typos.
bool inside = s.Substring(1, s.Length-2).Contains("*");

Open in new window

0
 
ToddBeaulieuCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AcumentalityCommented:
For 0 or 1:
^[\*]?[^\*]*[\*]?

Open in new window

0
 
AcumentalityCommented:
Actually...
^[\*]?[^\*]*[\*]?$

Open in new window

0
 
ToddBeaulieuCommented:
Acumentality: of course! :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now