- 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
CPU
1.2s
MEM
70M
1.1s
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
}
}
repl <a href="word">repl</word>repl repl
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 같이 보기[ | ]
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- Python (25)
- PHP (8)
- 정규표현식 (1)
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ―Pinkcrimson
유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong