함수 preg_replace()


preg_replace()
gsub()

1 Java[ | ]

Java
CPU
1.3s
MEM
71M
1.1s
Copy
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
    }
}
135.79.246.x
Java
Copy
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
    }
}
Loading

2 JavaScript[ | ]

JavaScript
Copy
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'));
The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy? 
The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy? 

3 Kotlin[ | ]

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

4 PHP[ | ]

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

5 Python[ | ]

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

6 Ruby[ | ]

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

7 같이 보기[ | ]