2012-01-06 37 views
5

Tôi muốn thao tác một Chuỗi trong Java bằng Regex. Mục tiêu là tìm tất cả các dấu hiệu $ có số chẵn là \ trước mặt chúng (hoặc không có) và sau đó thêm một số \ khác.Regex (Java) để tìm tất cả các ký tự đứng trước bởi một số chẵn các ký tự khác

Ví dụ:

"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" 

nên kết quả trong:

"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" 

Lý do ở đây là: một số $ đã trốn thoát với một \ và một số thoát \ có thể trong chuỗi cũng theo hình thức của \\. Tôi cần phải thoát khỏi số $ còn lại.

Trả lời

8

này nên làm việc: thay thế:

(^|[^\\])(\\{2})*(?=\$) 

với toàn bộ văn bản phù hợp (trừ lookahead), tiếp theo là \\.

Minh họa trong perl:

$ perl -pe 's,(^|[^\\])(\\{2})*(?=\$),$&\\,g' 
"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" # in... 
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out 
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # in... 
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" # out 

Với Java, toàn bộ trận đấu văn bản là $0. Mẫu mã:

// package declaration skipped 
import java.util.regex.Pattern; 

public final class TestMatch 
{ 
    private static final Pattern p 
     = Pattern.compile("(^|[^\\\\])(\\\\{2})*(?=\\$)"); 

    public static void main(final String... args) 
    { 
     String input = "\"$ Find the $ to \\$ escape \\\\$ or not \\\\\\$ " 
      + "escape \\\\\\\\$ like here $\""; 

     System.out.println(input); 

     // Apply a first time 
     input = p.matcher(input).replaceAll("$0\\\\"); 
     System.out.println(input); 

     // Apply a second time: the input should not be altered 
     input = p.matcher(input).replaceAll("$0\\\\"); 
     System.out.println(input); 
     System.exit(0); 
    } 
} 

Output:

"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" 
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" 
"\$ Find the \$ to \$ escape \\\$ or not \\\$ escape \\\\\$ like here \$" 

Một lời giải thích chút về regex sử dụng là theo thứ tự:

   # begin regex: 
(    # start group 
    ^   # find the beginning of input, 
    |   # or 
    [^\\]  # one character which is not the backslash 
)    # end group 
       # followed by 
(    # start group 
    \\{2}  # exactly two backslashes 
)    # end group 
*    # zero or more times 
       # and at that position, 
(?=    # begin lookahead 
    \$   # find a $ 
)    # end lookahead 
       # end regex 

Để được thực sự hoàn chỉnh, đây là những vị trí mà tại đó các regex động cơ sẽ tìm thấy văn bản phù hợp (được biểu thị bằng <>) và vị trí con trỏ (được biểu tượng bằng |):

# Before first run: 
|"$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" 
# First match 
"<>|$ Find the $ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" 
# Second match 
"$ Find the <>|$ to \$ escape \\$ or not \\\$ escape \\\\$ like here $" 
# Third match 
"$ Find the $ to \$ escape <\\>|$ or not \\\$ escape \\\\$ like here $" 
# Fourth match 
"$ Find the $ to \$ escape \\$ or not \\\$ escape <\\\\>|$ like here $" 
# From now on, there is no match 
0

Tôi nghĩ rằng một cái gì đó như thế này có thể làm việc:

\$(\\\\)*\\ 
Các vấn đề liên quan