Example of a simple application of regular expressions in central view [java-based]

Source: Internet
Author: User

Example of a simple application of regular expressions in central view [java-based]

This article describes the simple application of regular expressions in central view. We will share this with you for your reference. The details are as follows:

Because the development work needs to filter the content in the text, delete or replace some useless or non-conforming information. As a result, we found a problem. In a certain type of engineering text, many units with the same writing style but different meanings need to be converted into Chinese characters corresponding to the actual meanings. For example, if the viscosity is 17 s and the moving distance is 350 cm, the concrete must be in the solid and external light. The vibrating time is 30 s. ", Obviously, the first s is the unit of viscosity, and the second s is the unit of time. Now we need to replace all the s that represent the time in the text with" s ". Under the guidance of a friend, it is found that the loop view in the regular expression can be used to conveniently implement this function.

In a regular expression, the general principle is that the location information is used for matching. In my opinion, it can also be understood as matching based on the context of the target character.

The specific Java code is as follows:

Package ccnu; import java. util. regex. matcher; import java. util. regex. pattern; import java. util. regex. patternSyntaxException; public class regex {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub String tempHtmlString = "the viscosity is 17 s, and the moving distance is 350 cm. The concrete must contain solid and external light. The vibrating time is 30 s. "; Pattern p_html; Matcher m_html; String regEx =" ([^ viscosity] [u4e00-u9fa5] {1, 10 }(? <= [0-9]) s ([^ a-z] $) "; p_html = Pattern. compile (regEx); m_html = p_html.matcher (tempHtmlString); String resultString = ""; try {resultString = m_html.replaceAll ("$1 second");} catch (PatternSyntaxException ex) {// Syntax error in the regular expression} catch (IllegalArgumentException ex) {// Syntax error in the replacement text (unescaped $ signs ?) } Catch (IndexOutOfBoundsException ex) {// Non-existent backreference used the replacement text} System. out. println (resultString );}}

Result:The viscosity is 17 s, and the moving distance is 350 cm. The concrete must be in the solid outer light. The vibrating time is 30 seconds.

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

I hope this article will help you learn regular expressions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.