.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

easiest way to do WCMATCH in .net?

9 REPLIES 9
Reply
Message 1 of 10
JamesMaeding
968 Views, 9 Replies

easiest way to do WCMATCH in .net?

I am aware of regex and several other string methods, but have not seen a built in way of checking against multiple patterns allowing for asterisks.

Curious what others have come up with.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

9 REPLIES 9
Message 2 of 10
Hallex
in reply to: JamesMaeding

Try asteriks preceded by "`" character, kinda "`*mybad"

if string.StartWith("`*mybad") etc

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 10
JamesMaeding
in reply to: Hallex

I see, escape the *, good tip.

I will try that also with the contains method, thanks


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 4 of 10
Hallex
in reply to: JamesMaeding

Yes, 'Contains' method will be handy in this case,

Cheers 🙂

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 10

Exactly the same as (wcmatch ...) in AutoLisp and acutWcMatch/acutWcMatchEx in ObjectARX doing:

 

Autodesk.AutoCAD.Internal.Utils.WcMatch(string str, string pattern);
Autodesk.AutoCAD.Internal.Utils.WcMatchEx(string str, string pattern, bool ignoreCase);

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 6 of 10

great suggestion, I saw your same post on theswamp from a while back.

I am actually doing a non-acad project, so have to use regular .net, but still cool to have access to acad's routines.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 7 of 10

Yes, but that's not the 'easiest' way Smiley Very Happy

 

namespace System
{
   public static class WcMatchExtensionMethods
   {
      public static bool Matches( this string str, string pattern, bool ignoreCase )
      {
         return Autodesk.AutoCAD.Internal.Utils.WcMatchEx( str, pattern, ignoreCase );
      }

      public static bool Matches( this string str, string pattern )
      {
         return Autodesk.AutoCAD.Internal.Utils.WcMatchEx( str, pattern, true );
      }
   }
}

 

Message 8 of 10
jeff
in reply to: JamesMaeding

Honestly I have never really used Autolisp or Regex either.

This is only from 5 minutes a messing around since I thought it might be good to see what Regex was about. I only knew it had something to do with matching character in strings, but taking a quick look at WCMATCH and this article Converting Wildcards to Regexes this might be a start, but there are guys with AutoLisp and Regex experience around here and other forums that would much know a much better approach.

 

A little console program playing around

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string poo = "I have no idea and I have spent 5 minutes on this and have no idea if it will work";
            Console.WriteLine(poo.WCMATCH("*z"));/////////////////////->False
            Console.WriteLine(poo.WCMATCH("*al"));////////////////////->False
            Console.WriteLine(poo.WCMATCH("min???s"));////////////////->True
            Console.WriteLine(poo.WCMATCH("*z", "*al", "min???s"));///->True
            Console.WriteLine(poo.WCMATCH("*z", "*al", "*ha"));///->True
                                                                   
            Console.ReadLine();
        }
    }

    public static class StringExtensions
    {
        public static bool WCMATCH(this string text, params string[] patterns)
        {
            foreach (string pat in patterns)
            {
                string reg = s_wildcardToRegex(pat);
                if (Regex.Match(text, reg).Success)
                {
                    return true;
                }
            }

            return false;
        }
///////////source http://www.codeproject.com/Articles/11556/Converting-Wildcards-to-Regexes
        private static string s_wildcardToRegex(string pattern)
        {
            return Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".");
        }
    }
}

 

 

 *********Edit**********

DiningPhilosopher slipped in while I was posting and prime of example of what I meant when I I said other guys would know much better than I would. Actually I never said that but typed it.

You can also find your answers @ TheSwamp
Message 9 of 10
JamesMaeding
in reply to: jeff

wow, thanks for all the responses.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 10 of 10


@DiningPhilosopher wrote:

Yes, but that's not the 'easiest' way 


Oh, Tony! Of course your version is beautiful and laconic. For this you have new kudos from me! Smiley Happy

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost