2012-01-06 11 views
5

Regex kullanarak Java'da bir String kullanmak istiyorum. Hedef, önündeki \ çift sayılı (veya hiçbiri) bir sayıya sahip olan $ işaretlerini bulmak ve ardından başka bir \ eklemektir.Regex (Java) ile eşit sayıda diğer karakterlerin önündeki tüm karakterleri bulmak için

Örnek:

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

neden olmalıdır: Burada

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

Gerekçe ise: Zaten bir \ ve bazı kaçış \ ile kaçan bazı $ şeklinde de dizede olabilir \\ Kalan $ dan kaçmam gerek.

cevap

8

: değiştirin:

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

\\ ardından (lookahead hariç) eşleştirilmiş tüm metin, ile. perl'de

İllüstrasyon: Java ile

$ 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 

, tüm metin maç $0 olduğunu. Örnek kod:

// 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); 
    } 
} 

Çıktı:

"$ 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 \$" 

kullanılan regex hakkında küçük bir açıklama sırada bulunuyor:

   # 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 

gerçekten tam olması için, burada pozisyonları hangi regex de motor eşleşen metni (<> ile sembolize edilmiş) ve imleç konumunu (| ile sembolize edilmiş) bulacaktır:

# 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

Böyle bir şey olabileceğini düşündükleri: Bu işi yapmak gerekir

\$(\\\\)*\\ 
İlgili konular