Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pattern to extract the required characters in the java background

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "java background how to use Pattern to extract the required characters", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java background how to use Pattern to extract the required characters" bar!

Write in front of dealing with the problem

Due to the iterative function of the project, the ID naming rules in the original page are different from the current naming rules (ps: since you want to use the original things, why the compatibility issues are not taken into account in the design, speechless), so you need to extract all the original ID.

How to extract the problems encountered?

After looking for many methods, I feel that using Pattern extraction is more in line with the requirements. So I started trying.

1. First, simply test String str = "{abc {def:} deftfha}"; Pattern p=Pattern.compile ("\\ {(\\ w+)\:"); Matcher m=p2.matcher (str2); while (m.find ()) {System.out.println (m.group (1));}

Okay, no problem. Extraction is normal. So take out some of the strings you need to extract for testing.

two。 Project content test String str = "\" EquipmentID\ ":\" SSC_FZ_DQ#MJ23JZ_FZ_CZ_CZGX#YL#SBBM\ ","; Pattern p=Pattern.compile ("\" EquipmentID\ ":\" (\\ w+)\ ","); Matcher m=p2.matcher (str2); while (m.find ()) {System.out.println (m.group (1));}

Here comes the problem. Nothing has been extracted. Then try to extract only the characters in EquipmentID, the test is no problem. The problem lies in regular matching. (\\ w +) is only applicable to intercept the text part, and change it to (. *), ok, which can intercept the SSC_FZ_DQ#MJ23JZ_FZ_CZ_CZGX#YL#SBBM part normally.

3. Carry on the actual operation

Establish a database connection.

Public class CopyOracle2MySQL1 {/ * source database, connection configuration of target database * / private final String DEST_MYSQL_JDBC_URL =; private final String SOURCE_JDBC_URL = ""; private final String SOURCE_JDBC_USER = ""; private final String SOURCE_JDBC_PASSWORD = "" Public void startImport () throws Exception {/ / create a connection to two databases Class.forName ("com.mysql.jdbc.Driver"); / / Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection connDest = DriverManager.getConnection (DEST_MYSQL_JDBC_URL); / / Connection connSource = DriverManager.getConnection (SOURCE_JDBC_URL, SOURCE_JDBC_USER, SOURCE_JDBC_PASSWORD) Try {/ / manually enter each table name (order is required to ensure that tables with foreign keys insert data after the main table) importTable (connDest, "APP_WGADDATA", "qtsc_jk_scjkzttxx");} finally {/ / automatically close database resource connDest.close (); / / connSource.close () }} private void importTable (Connection connDest, String oracleTableName,String mysqlTableName) throws Exception {Statement stmt = null; try {stmt = connDest.createStatement (); String mysqlSql = "select ZTTNR from qtsc_jk_scjkzttxx qjs where qjs.ZTTID = '0284fcbdcdbd4da3bdef78ed769515c6'"; String mysqlSql1 = "select ZTTNR from qtsc_jk_scjkzttxx"; ResultSet rs = stmt.executeQuery (mysqlSql1) Map sbbmMap = new HashMap (); while (rs.next ()) {String zttnr = rs.getString (1); / / System.out.println (zttnr); Pattern p = Pattern.compile ("\" EquipmentID\ ":\" (. *)\ ",\" UnitName\ "); Matcher m=p.matcher (zttnr) While (m.find ()) {System.out.println (m.group (1));} / / System.out.println (sbbmMap);} / / calculate the SQL statement z ResultSetMetaData rsmd = rs.getMetaData () of the PreparedStatement of the target database first; rs.close () } catch (Exception e) {e.printStackTrace ();} finally {if (stmt! = null) {stmt.close ();} public static void main (String [] args) throws Exception {CopyOracle2MySQL1 ins = new CopyOracle2MySQL1 (); ins.startImport ();}

The test results show that the extraction is carried out in the early stages of all compliance with the rules, and it is found that the last "UnitName" is automatically matched, which is obviously the problem of the rules, so the problem is solved by changing (.) to (.).

Attachment: JAVA Pattern regular get the content in curly braces

Use regular expressions to get the desired value in the string:

Get string scene: Hello (hee hee)

I need to get "hee hee". How can I get it?

1. Get through a regular expression:

String str = "Hello (hee hee)"; Pattern p = Pattern.compile ("\\ ([^\]] +)"); Matcher matcher = p.matcher (str); if (matcher.find () & & matcher.groupCount () > = 1) {System.out.println (matcher.group (1));}

two。 Obtain by character interception:

String str = "Hello (hee hee)"; String xixi = str.substring (str.indexOf ((") + 1, str.indexOf ("); System.out.println (xixi) Thank you for your reading, the above is the "java background how to use Pattern to extract the required characters" content, after the study of this article, I believe you on the java background how to use Pattern to extract the required characters of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report