关于匹配本文中几小时几分钟的问题
一段文本可能会出现这样几种情况:
用了 1小时43分钟
用了 1小时
用了 43分钟
要实现的是从文本中匹配出这个时间,即加粗部分
现在想到的正则表达式是preg_match('/([0-9]+小时[0-9]+分钟|[0-9]+小时|[0-9]+分钟)/is', $txt, $matches)
虽然能够正确匹配出想要的结果,但是感觉不够简洁,请问如何改进?
回复讨论(解决方案)
加粗只是视觉效果,分开来加并无影响
$s =<<< TXT一段文本可能会出现这样几种情况:用了1小时43分钟用了1小时用了43分钟TXT;echo preg_replace('/\d+(小时|分钟)/', '<b>$0</b>', $s);
一段文本可能会出现这样几种情况:用了<b>1小时</b><b>43分钟</b>用了<b>1小时</b>用了<b>43分钟</b>
我不是为了加粗,是为了提取出加粗部分这样子的文字。会有三种情况,就是包含小时和分钟、只有小时、只有分钟。
加粗只是视觉效果,分开来加并无影响
$s =<<< TXT一段文本可能会出现这样几种情况:用了1小时43分钟用了1小时用了43分钟TXT;echo preg_replace('/\d+(小时|分钟)/', '<b>$0</b>', $s);
一段文本可能会出现这样几种情况:用了<b>1小时</b><b>43分钟</b>用了<b>1小时</b>用了<b>43分钟</b>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go Language Regular Expressions Practical Guide: How to Match Hexadecimal Color Codes Introduction: Regular expressions are a powerful and flexible tool for pattern matching and finding strings. In Go language, we can use the built-in regular expression package regexp to implement these operations. This article will introduce how to use regular expressions to match hexadecimal color codes in Go language. Importing the regular expression package First, we need to import the regular expression package regexp of the Go language. You can add the following import statement at the beginning of the code: i

PHP regular expression practice: matching letters and numbers Regular expression is a tool used to match strings, which can easily realize string search, replacement, split and other operations. Regular expressions are also a very useful tool in PHP development. This article will introduce how to use PHP regular expressions to match letters and numbers. Matching a Single Character To match a single character, you can use the character classes in regular expressions. Character classes are represented by square brackets []. The characters in them represent the characters that can be matched. You can use hyphens - to represent ranges.

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

Jedi Submarine 2 is a third-person shooting game with high-quality masterpiece gameplay. It has a lot of exciting gameplay that allows friends to explore the operational fun of online shooting battles. The online mode in the game can be matched. Some players I still don’t know how to operate matching. In this issue, I will share the matching steps with you! Matching operation tutorial of Jedi Submarine 2. Answer: Click Quick Match on the planet interface. The matching method of Jedi Submarine 2. The quick matching of Jedi Submarine 2 is a very good function. It can help players find teammates to match together, enter a mission together, and cooperate with each other to obtain a higher mission evaluation. The matching options are on the planet interface. When looking for tasks or viewing public rooms, there will be a quick match below. Click to start matching. If the player turns on cross leveling

As a strongly typed language, Java requires that the types of variables must be clearly determined at compile time, which ensures the security of the program to a certain extent. But sometimes, at runtime, we may encounter a type conversion exception - java.lang.ClassCastException. This exception will appear in a Java program. When the program tries to convert an object to an incompatible type, This exception will be thrown. Java.lang.ClassCastExcepti

HTML is a commonly used page markup language used to display content on web pages. In HTML, the textarea tag is used to create text boxes that allow users to enter or edit text. When you need to extract all textarea tags and their contents from a page, PHP regular expressions can provide a simple and effective solution. In this article, we will learn how to match all textarea tags in HTML using PHP regular expressions. Understand regular tables

Regular expressions are a powerful text processing tool that are widely used in PHP. One common usage is to match multiple consecutive specific characters, such as matching multiple consecutive spaces, multiple consecutive commas, etc. This article will introduce how to use regular expressions in PHP to achieve this function. In PHP, we can use the preg_match() function to perform regular expression matching. This function requires two parameters: the regular expression and the string to be matched. If the match is successful
