The invention discloses a method and a device for matching a longest prefix. The method comprises the following steps: A, reading a search node; B, determining whether the offset domain of the read search node indicates that an upper node has the matched prefix or not, adding the offset domain of the search node to a pointer pointing to a leaf node array in the upper node if the upper node has the matched prefix, updating the current best matching pointer and executing the step C, and executing the step C if the upper node has no matched prefix; C, determining whether the search node has the leaf node or not while determining corresponding bit matching of branch indication domain and search key words of the search node; D, reading the internal bitmap of the search node while determining that the search node does not have the leaf node, computing the longest matching prefix existing in the search node according to the internal bitmap and the pointer pointing to the leaf node array in the search node, updating the current best matching pointer, and computing the address of the leaf node corresponding to the current best matching pointer. The method can improve the search speed.