Get the App
SLTechnology News&Howtos  ›  Development  › 

Comparison of regular expressions between Python and Java

Shulou Source: shulou.com Published: 2022-06-03 14:14:02 11月14日 Update

This article mainly explains "the comparison of regular expressions between Python and Java". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the comparison of regular expressions between Python and Java".

Catalogue

Simple batch replacement

Complex template replacement

Summary

Simple batch replacement

Example: replace and in batches with & &

Python implementation

Import redef 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 implementation

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 complex template replacement

For example: replace x in (1pm 2) in batches with [1JI 2] .quote (x)

Analysis: templated

Enter packet capture (\ S+)\ s+in\ s *\ ((. +?)\)

Fill in [@ 2] .packets (@ 1)-@ 1 and @ 2 corresponding to group 1 and group 2 in the packet capture, respectively.

Python implementation

Import redef 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 inTextif _ _ name__ = = "_ _ main__": fromRegex = "(\ S+)\ s+in\ s*\ ((. +?)\)" toText = "[@ 2] .in (@ 1)" inText = "x in (1pr 2) and y in (3pr 4)" outText22 = transformComplex (fromRegex, toText, inText) print (outText22) # # OUTPUT: [1Jue 2].

Java implementation

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)

Tags: Regular expression template grouping learning complexity content that is ideas situations articles more catalogs knowledge knowledge points articles follow problems analysis practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno MySQL NVidia vpn