Finding string with "wild" characters in another string

Discussion in 'C++' started by Pawe³, Jul 9, 2004.

  1. Pawe³

    Pawe³ Guest

    Hello!

    I'm looking for efficient code or site where I can find code for finding one
    string in another string. String which I search should have "wild"
    characters like '?' for any one char and '*' for any string of characters.

    I'm looking for way to effective getting string from text file and then
    searching it like I write above.

    Thanks in advance for any helps, notices or sites

    --

    Best regards
    Pawe³ Chmielarz
    ----------------------------------
     
    Pawe³, Jul 9, 2004
    #1
    1. Advertising

  2. "Pawe³" <> wrote in message
    news:cclos8$fb8$...
    > Hello!
    >
    > I'm looking for efficient code or site where I can find code for finding

    one
    > string in another string. String which I search should have "wild"
    > characters like '?' for any one char and '*' for any string of characters.
    >
    > I'm looking for way to effective getting string from text file and then
    > searching it like I write above.
    >
    > Thanks in advance for any helps, notices or sites
    >



    http://www.boost.org/libs/regex/doc/index.html

    john
     
    John Harrison, Jul 9, 2004
    #2
    1. Advertising

  3. Pawe³

    Roman Ziak Guest

    "Pawe³" <> wrote in message
    news:cclos8$fb8$...
    > Hello!
    >
    > I'm looking for efficient code or site where I can find code for finding

    one
    > string in another string. String which I search should have "wild"
    > characters like '?' for any one char and '*' for any string of characters.
    >
    > I'm looking for way to effective getting string from text file and then
    > searching it like I write above.
    >
    > Thanks in advance for any helps, notices or sites


    Hope this helps. I pulled it out of the project with some custom types
    (e.g. char*) and changed them here to built-in types, but apologize if
    it won't compile as is. The algorithm has been tested and is in production.
    Unfortunately you will not get the found matched substrings as with
    regular expressions, but is quite fast.

    int wildMatch(char *wild, char *text)
    {
    enum { UNSYNC,SYNC,START };
    char *lastwild, *lasttext; /* last synced position in CMP mode */
    unsigned state, size; /* SEARCH mode(0), CMP mode otherwise */

    for(state=START; ; )
    {
    if(*wild == 0) { /* is end of wildargs ? */
    if(state!=UNSYNC && *text==0) /* is there text in synced mode?
    */
    return 0;
    if(state == START) /* there was no asterisk at all */
    return -1;
    /* compiler gives warning: possible use before definition */
    size = wild-lastwild; /* size of the string */
    return strcmp(lastwild, lasttext+strlen(lasttext)-size);
    }

    if(*text == 0) { /* is end of text ? */
    while(*wild=='*') wild++; /* skip all asterisks */
    return *wild==0 ? 0 : 1; /* wild is at the end - success */
    }

    if(state != UNSYNC)
    {
    /* sync is when texts are synchronized */
    if(*wild == '*') {
    while(*wild=='*') wild++;/* skip all consequent asterisks */
    lastwild = wild; /* remember last positions */
    lasttext = text;
    state = UNSYNC; /* unsynchronize */
    } else
    if(*wild == *text || *wild == '?') {
    wild++; /* they are same, move to nexts */
    text++;
    } else {
    if(state == START) /* they don't match (no wildcard) */
    return *wild-*text;
    wild = lastwild; /* not equal chars in CMP mode */
    text = ++lasttext; /* go back to last good */
    state = UNSYNC; /* unsynchronize */
    }
    }
    else
    {
    /* non-sync is looking first match */
    if(*wild == *text) {
    lasttext = text; /* remember last good position */
    state = SYNC; /* synchronized again */
    } else
    text++;
    }
    }
    }
     
    Roman Ziak, Jul 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SatishPasala

    Removing wild characters from a string

    SatishPasala, Nov 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,714
    SatishPasala
    Nov 29, 2005
  2. Pawe³
    Replies:
    9
    Views:
    364
    Dan Pop
    Jul 13, 2004
  3. W. eWatson

    Wild Card String Comparison

    W. eWatson, Aug 28, 2008, in forum: Python
    Replies:
    6
    Views:
    500
    W. eWatson
    Aug 28, 2008
  4. Mat Schaffer

    Wild ideas: Finding a missing 'end'

    Mat Schaffer, Jul 29, 2006, in forum: Ruby
    Replies:
    6
    Views:
    203
    Paul Battley
    Jul 29, 2006
  5. Junkone
    Replies:
    4
    Views:
    179
    Eric Hodel
    Jun 16, 2008
Loading...

Share This Page