Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

unknown order

Last post 06-13-2007, 3:50 PM by Stevezilla00. 17 replies.
Page 1 of 2 (18 items)   1 2 Next >
Sort Posts: Previous Next
  •  06-12-2007, 6:56 AM 31787

    unknown order

    How do I match any sequence of known characters (a, b ,c and d for example) in any order without explicitly specifying all combinations?

    For example I want to match: abcd, dcab, abdc, bcda, etc.

    Thanks.

  •  06-12-2007, 8:26 AM 31789 in reply to 31787

    Re: unknown order

    I think its again the "cats and dogs" problem but I can`t find the thread anymore:

    But I got the regex in my "important-regex" text file Big Smile

    so your regex should look like this if nobody finds an easier way or shows that this one fails on some data.

    ^(?=.*?a)(?=.*?b)(?=.*?c)(?=.*?d).*?$

     

     

  •  06-12-2007, 8:30 AM 31790 in reply to 31787

    Re: unknown order

    for that u use a character class, which consists of 4 characters: [abcd]

    next u specify the length of the match pattern u r looking for: length=4: {4}

    thus arriving at the pattern u'd want ot use:

    ^[abcd]{4}$

    it'll match on abcd, dcab, abdc, bcda, etc.

    note ^ and $ here: the start and the end of the line/input, meaning that in this scenario u will be matching your strings one-at-a-time, on a separate line OR in a text box where it sits alone. IF u want ot match your strings in a piece of text, i.e. as parts of a bigger text, then startegy will have to be changed. Usually it will get more involved.

  •  06-12-2007, 8:37 AM 31791 in reply to 31789

    Re: unknown order

    docdawson:

    I think its again the "cats and dogs" problem but I can`t find the thread anymore:

    But I got the regex in my "important-regex" text file Big Smile

    so your regex should look like this if nobody finds an easier way or shows that this one fails on some data.

    ^(?=.*?a)(?=.*?b)(?=.*?c)(?=.*?d).*?$

    hi, docdowson,

    i think u misinterpreted the task. The poster is looking for combinations of 4 characters. Your regex will match on any combination of characters including four mandatory ones: a, b,c,d,

    i.e your regex 'll match on say

    aXbZcSdZ

    which does not comply with the original spec

  •  06-12-2007, 9:58 AM 31794 in reply to 31791

    Re: unknown order

    hi sergeiZ

    you are right, the cats and dogs regex is wrong here. my test cases i have done where only "abcd", "bcad", etc Embarrassed

    but i think yours  will match "aaaa" too. i thought the first second on this simple regex too, but had no idea to match each character only one time no matter which order.
     

  •  06-12-2007, 10:39 AM 31795 in reply to 31794

    Re: unknown order

    Eh, what's up doc? Smile

    Here's a pattern that should match http://regexlib.com/REDetails.aspx?regexp_id=564

    This question has come up before in the last few years.  In fact I'm pretty sure Sergei came up with an alternate solution to my version that in this forum somewhere.

    The only issue is the regex grows very fast for each new letter you add 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  06-12-2007, 10:49 AM 31796 in reply to 31794

    Re: unknown order

    u r right: i overlooked the req that all 4 chars are mandatory in the string. we can do it by adding 4 look-aheads:

    (?im)^(?=.*a)(?=.*b)(?=.*c)(?=.*d)[abcd]{4}$

    now

    aaaa

    will not be matched.

  •  06-12-2007, 6:14 PM 31802 in reply to 31796

    Re: unknown order

    I am curious what would be the pattern implementation of this in VBSCRIPT.


  •  06-12-2007, 7:44 PM 31803 in reply to 31802

    Re: unknown order

    ddrudik:
    I am curious what would be the pattern implementation of this in VBSCRIPT.

    If using Sergei's approach, it should be the same minus the leading "(?im)" which is meant to enable case-insensitive matching and multiline mode. Of course, you'll have to reenable those modifiers however that's done in VBScript.


    My regex-centric blog :: JavaScript regex tester
  •  06-12-2007, 9:37 PM 31807 in reply to 31803

    Re: unknown order

    That's actually what I suspected, so I tried:

    ^(?=.*a)(?=.*b)(?=.*c)(?=.*d)[abcd]{4}$

    But with:

    Set regEx = New RegExp
    regEx.Global = True
    regEx.IgnoreCase = True
    regEx.MultiLine = True
    teststring = "abcd"
    regEx.Pattern = "^(?=.*a)(?=.*b)(?=.*c)(?=.*d)[abcd]{4}$"
    Set Test = regEx.Test(teststring)

    Test is False.

    I suspect that VBSCRIPT does not support suffix (or prefix) exclusion matching (or matching if not present).  That's something that hasn't worked for me in the past so I am curious if there's a change I can make to get it to work.


  •  06-12-2007, 9:55 PM 31808 in reply to 31807

    Re: unknown order

    ddrudik:
    I suspect that VBSCRIPT does not support suffix (or prefix) exclusion matching (or matching if not present).

    I'm not sure what you mean by this. Do you mean leading or trailing lookaround? If so, you might want to try mash's pattern (which he linked to above), since it doesn't use lookarounds at the very beginning or end of the pattern. To make that pattern work in VBScript, you'll probably have to remove the leading "(?i:" and the very last ")". But maybe I misunderstood you, and if so I apologize.


    My regex-centric blog :: JavaScript regex tester
  •  06-12-2007, 10:22 PM 31809 in reply to 31808

    Re: unknown order

    It is the leading and trailing lookaround that I was referring to, and from looking at mash's pattern this question was answered back in 2004.  It's a good reminder to search for old patterns before asking new questions.
  •  06-13-2007, 1:58 AM 31811 in reply to 31795

    Re: unknown order

    mash:

    Eh, what's up doc? Smile

    Here's a pattern that should match http://regexlib.com/REDetails.aspx?regexp_id=564

    This question has come up before in the last few years.  In fact I'm pretty sure Sergei came up with an alternate solution to my version that in this forum somewhere.

    The only issue is the regex grows very fast for each new letter you add 

    Hi mash,

    I rarely search in this database. My fault again Embarrassed  . But good to know that this is an solution because I tried (sadly without success) this way too. And thats another sign for me that I`m just at the beginning of being able to create good and solid regex.

    what is the plural from regex?

  •  06-13-2007, 2:06 AM 31812 in reply to 31809

    Re: unknown order

    ddrudik:
    It is the leading and trailing lookaround that I was referring to, and from looking at mash's pattern this question was answered back in 2004.  It's a good reminder to search for old patterns before asking new questions.

    Ah 2004, it was a very good year, for answers to this thread. Smile

    Ddrudik, this is the infamous IE lookahead bug http://regexadvice.com/blogs/mash/archive/2004/10/05/320.aspx I found also in 2004. Basically Microsoft's scripting engines don't handle multiple look-aheads correctly.  The blog entry has a more detailed explanation.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  06-13-2007, 2:19 AM 31813 in reply to 31811

    Re: unknown order

    docdawson:
    mash:

    Eh, what's up doc? Smile

    Here's a pattern that should match http://regexlib.com/REDetails.aspx?regexp_id=564

    This question has come up before in the last few years.  In fact I'm pretty sure Sergei came up with an alternate solution to my version that in this forum somewhere.

    The only issue is the regex grows very fast for each new letter you add 

    Hi mash,

    I rarely search in this database. My fault again Embarrassed  . But good to know that this is an solution because I tried (sadly without success) this way too. And thats another sign for me that I`m just at the beginning of being able to create good and solid regex.

    what is the plural from regex?

    Don't sweat it Doc, I didn't come up with that solution right away when the question was first asked of me.  In fact I didn't think it could be done, the solution just hit me out of the blue months, yes months, later. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
Page 1 of 2 (18 items)   1 2 Next >
View as RSS news feed in XML