.NET

Reply
Distinguished Contributor
1,084 Posts
56 Kudos
Registered: ‎12-27-2006
Post 1 of 10

easiest way to do WCMATCH in .net?

275 Views, 9 Replies
03-25-2013 09:27 AM

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()
http://onesmallstepforcad.com/

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 10:00 AM in reply to: jmaeding

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

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

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
1,084 Posts
56 Kudos
Registered: ‎12-27-2006
Post 3 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 10:03 AM 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()
http://onesmallstepforcad.com/

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 4 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:11 AM in reply to: jmaeding

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

Cheers :smileyhappy:

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Moderator
1,464 Posts
277 Kudos
Registered: ‎04-09-2008
Post 5 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:20 AM in reply to: jmaeding

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);

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
1,084 Posts
56 Kudos
Registered: ‎12-27-2006
Post 6 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:57 AM in reply to: Alexander.Rivilis

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()
http://onesmallstepforcad.com/

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 7 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 03:33 PM in reply to: Alexander.Rivilis

Yes, but that's not the 'easiest' way :smileyvery-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 );
      }
   }
}

 

Valued Mentor
340 Posts
53 Kudos
Registered: ‎05-12-2009
Post 8 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 03:50 PM in reply to: jmaeding

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
Distinguished Contributor
1,084 Posts
56 Kudos
Registered: ‎12-27-2006
Post 9 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 04:00 PM in reply to: jeff

wow, thanks for all the responses.

internal protected virtual unsafe Human()
http://onesmallstepforcad.com/

Moderator
1,464 Posts
277 Kudos
Registered: ‎04-09-2008
Post 10 of 10

Re: easiest way to do WCMATCH in .net?

03-25-2013 05:12 PM in reply to: DiningPhilosopher

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! :smileyhappy:

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post