함수 preg_replace()


preg_replace()
gsub()

1 Java[ | ]

public class MyClass {
    public static void main(String args[]) {
        String str = "135.79.246.80";
        String pattern = "(\\.\\d+)$";
        String replacement = ".x";
        System.out.println( str.replaceAll(pattern, replacement) ); // 135.79.246.x
    }
}
public class MyClass {
    public static void main(String args[]) {
        String str = "word <a href=\"word\">word</word>word word";
        str = str.replaceAll("word(?!([^<]+)?>)", "repl");
        System.out.println(str); // repl <a href="word">repl</word>repl repl
    }
}

2 JavaScript[ | ]

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));

3 Kotlin[ | ]

fun main(args: Array<String>) {
    println( "135.79.246.80".replace("\\.\\d+$".toRegex(), ".x") ) // 135.79.246.x
}

4 PHP[ | ]

$string = '135.79.246.80';
$pattern = '/(\.\d+)$/';
$replacement = '.x';
echo preg_replace($pattern, $replacement, $string); # 135.79.246.x

5 Python[ | ]

import re
text = '135.79.246.80';
pattern = r'(\.\d+)$';
replacement = '.x';
result = re.sub(pattern, replacement, text)
print( result )

6 Ruby[ | ]

str1 = 'The quick brown fox jumped over the lazy dog.'
puts str1.gsub(/quick/, 'slow') # The slow brown fox jumped over the lazy dog.

7 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}