Regular expressions and string matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DennyLoi
    New Member
    • Nov 2007
    • 10

    #1

    Regular expressions and string matching

    Hi everyone,

    Here is my problem, I have a partially decrypted piece string which would appear something like.

    Partially deycrpted: the?anage??esid eshe?e
    Plain text: themanagerresid eshere


    So you can see that there are a few letter missing from the decryped text. What I am trying to do it insert spaces into the string so that I get:

    The ?anage? ?esides he?e

    I have a method which splits up the string in substrings of varying lengths and then compares the substring with a word from a dictionary (implemented as an arraylist) and then inserts a space.

    The problem is that my function does not find the words in the dictionary because my string is only partially decryped.

    Eg: ?anage? is not stored in the dictionary, but the word “manager” is.

    So my question is, is there a way to build a regular expression which would match the partially decrypted text with a word from a dictionary (ie - ?anage? is recognised and “manager” from the dictionary).
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by DennyLoi
    So my question is, is there a way to build a regular expression which would match the partially decrypted text with a word from a dictionary (ie - ?anage? is recognised and “manager” from the dictionary).
    Yes you can do that: the regular expression ".anage." matches "manager" but
    you have to check all your words in your dictionary for a possible match which
    can be quite slow. Google for "PATRICIA" trie and see how this can be done a
    bit more clever. Your dictionary needs probably be changed structurally for that.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by DennyLoi
      Hi everyone,

      Here is my problem, I have a partially decrypted piece string which would appear something like.

      Partially deycrpted: the?anage??esid eshe?e
      Plain text: themanagerresid eshere


      So you can see that there are a few letter missing from the decryped text. What I am trying to do it insert spaces into the string so that I get:

      The ?anage? ?esides he?e

      I have a method which splits up the string in substrings of varying lengths and then compares the substring with a word from a dictionary (implemented as an arraylist) and then inserts a space.

      The problem is that my function does not find the words in the dictionary because my string is only partially decryped.

      Eg: ?anage? is not stored in the dictionary, but the word “manager” is.

      So my question is, is there a way to build a regular expression which would match the partially decrypted text with a word from a dictionary (ie - ?anage? is recognised and “manager” from the dictionary).
      The * wildcard is your friend here. You can read more about it.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by r035198x
        The * wildcard is your friend here. You can read more about it.
        Erm, make that a dot instead of a star. Regular expressions are not globbing.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          Erm, make that a dot instead of a star. Regular expressions are not globbing.

          kind regards,

          Jos
          No they are not.
          . it is.
          From my very own link!



          Originally posted by www.proftpd.org/docs/howto/Regex.html
          These leaves us with:
          • Replace any * characters with .*
          • Replace any ? characters with .
          • Leave square brackets as they are.
          • Replace any characters which are metacharacters with a backslashified version.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            No they are not.
            . it is.
            From my very own link!
            Hrmph; your quote is gone in my reply; but that's not how Posix regular expressions
            work: a dot is a single char that matches anything and a star is a reflexive closure
            of the previous regular expression (i.e. the regex to the left of it). What you described
            resembles a globbing pattern to a regular expression conversion.

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              ...What you described
              resembles a globbing pattern to a regular expression conversion.

              kind regards,

              Jos
              That's what was being explained at the point that I extracted.
              Which is what I should have done(the conversion that is).

              Comment

              • DennyLoi
                New Member
                • Nov 2007
                • 10

                #8
                I wrote the following method in order to test the matching using . in my regular expression.

                public void getWords(int y)
                {
                int x = 0;
                for(y=y; y < buff.length(); y++){
                String strToCompare = buff.substring( x,y); //where buff holds the partially decrypted text
                x++;
                Pattern p = Pattern.compile (strToCompare);
                for(int z = 0; z < dict.size(); z++){
                String str = (String) dict.get(z); //where dict hold all the words in the dictionary
                Matcher m = p.matcher(str);
                if(m.matches()) {
                System.out.prin tln(str);
                System.out.prin tln(strToCompar e);
                }}}
                // System.out.prin tln(buff);
                }

                If I run the method where my parameter = 12, I am given the following output.

                aestheticism
                aestheti.is.
                demographics
                de.o.ra.....

                Which suggests that the method is working correctly.

                However, after running for a short time, the method cuts and gives me the error:
                PatternSyntaxEx ception:
                Null(in java.util.regex .Pattern).

                Does anyone know why this would occur?

                Comment

                Working...