2012-01-06 9 views
5

मैं रीगेक्स का उपयोग कर जावा में एक स्ट्रिंग में हेरफेर करना चाहता हूं। लक्ष्य सभी $ संकेतों को ढूंढना है जिनके पास \ उनके सामने (या कोई नहीं) है और फिर \ जोड़ें।रेगेक्स (जावा) अन्य पात्रों की संख्या से पहले सभी वर्णों को खोजने के लिए

उदाहरण:

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

में परिणाम चाहिए: यहाँ

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

दलील है: कुछ $ पहले से ही एक \ और कुछ बच \ के साथ भाग रहे हैं के रूप में रूप में अच्छी तरह स्ट्रिंग में हो सकता है का \\। मुझे शेष $ से बचने की जरूरत है।

उत्तर

8

यह काम करना चाहिए: बदलें।

पर्ल में

उदाहरण:

$ 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 
जावा के साथ

, पूरे पाठ मैच $0 है। नमूना कोड:

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

आउटपुट:

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

इस्तेमाल किया regex के बारे में एक छोटी सी स्पष्टीकरण क्रम में है:

   # 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 

वास्तव में पूरा होने के लिए, यहां स्थितियां दी गई हैं जो regex पर इंजन मिलान करने वाला टेक्स्ट मिलेगा (<> के साथ प्रतीक) और कर्सर की स्थिति (| द्वारा प्रतीकात्मक):

# 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

मुझे लगता है कि कुछ इस तरह काम कर सकते हैं: मिलान किया (अग्रदर्शी को छोड़कर) पूरे पाठ, \\ द्वारा पीछा के साथ

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

:

\$(\\\\)*\\ 
संबंधित मुद्दे