人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

一篇文章帶你了解Python和Java的正則表達(dá)式對比

發(fā)布日期:2022-01-03 22:36 | 文章來源:站長之家

參考資料:

  1. 正則表達(dá)式語法–菜鳥教程
  2. Java正則表達(dá)式實現(xiàn)

簡單批量替換

舉例:將and 批量替換為&&

Python實現(xiàn)

import re
def transformSimple(fromRegex, toText, inText):
 return re.sub(fromRegex, toText,inText, flags =re.I)
if __name__ == "__main__":
 inText = "x =1 and y =2"
 fromRegex = " and "
 toText = " && "
 outText = transformSimple(fromRegex,toText,inText )
 print(outText)
	## OUTPUT: x =1 && y =2

Java實現(xiàn)

import java.util.*;
import java.util.regex.*;
public class RegexTest {
	private static String transformSimple(String regexPattern, String replText, String inText){
  return Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE).matcher(inText).replaceAll(replText);
 }
 public static void main(String[] args) {
	 String input = "x =1 and y =2";
  String patternString =" and ";
  String toText = " && ";
  String outText ="";
  outText = transformSimple(patternString, toText, input);
  System.out.println("RESULT: " + outText);
}
// RESULT: x =1 && y =2

復(fù)雜模板替換

舉例:將x in (1,2)批量替換為[1,2].contains(x)

分析: 模板化

  • 輸入分組捕獲 (\S+)\s+in\s*\((.+?)\)
  • 輸出分組填寫 [@2].contains(@1) – @1和@2分別對應(yīng)分組捕獲中的第1組和2組。

Python實現(xiàn)

import re
def transformComplex(fromRegex, toText, inText):
 regObj = re.compile(fromRegex, flags =re.I)
 for match in regObj.finditer(inText):
  index = 1
  outText = toText
  for group in match.groups():
outText = outText.replace("@"+str(index), group)
index +=1
  inText = inText.replace(match.group(0), outText)
 return inText
if __name__ == "__main__":
 fromRegex = "(\S+)\s+in\s*\((.+?)\)"
 toText = "[@2].contains(@1)"
 inText = "x in (1,2) and y in (3,4)"
 outText22 = transformComplex(fromRegex, toText, inText)
 print(outText22)
 ## OUTPUT: [1,2].contains(x) and [3,4].contains(y)

Java實現(xiàn)

import java.util.*;
import java.util.regex.*;
public class RegexTest {
	private static String transformComplex(String regexPattern, String replText, String inText){
  Pattern pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(inText);
  String outText ="";
  while (matcher.find()){
outText =  replText;
for (int i =1; i <= matcher.groupCount(); i++){
 outText = outText.replace("@"+i, matcher.group(i));
}
inText = inText.replace(matcher.group(0), outText);
  }
  return inText;
 }
 public static void main(String[] args) {
  String input = "x in (1,2) and y in (3,4)";
  String patternString ="(\\S+)\\s+in\\s*\\((.+?)\\)";
  String toText = "[@2].contains(@1)";
  String outText ="";
  outText = transformComplex(patternString, toText, input);
  System.out.println("RESULT: " + outText);
 }
}
// RESULT: [1,2].contains(x) and [3,4].contains(y)

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部