In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.