


This article mainly introduces regular expressionslookaround before and after the tutorial, and analyzes the implementation techniques and notes of the forward lookup and backward lookup functions based on specific problems. Friends in need can refer to
The examples in this article describe the lookaround before and after the regular expression tutorial. Share it with everyone for your reference, as follows:
Note: In all examples, the regular expression matching results contain [ and ]## in the source text. #, some examples will be implemented using Java. If it is the usage of regular expressions in Java itself, it will be explained in the corresponding place. All java examples are tested under JDK1.6.0_13.
1. Introduction to the problem
In an HTML page, match the text between a pair of tags, such as matching the tags of the page, that is, and : Text:.*? [Tt][Ii][Tt][Ll][Ee ]>
Result: 【2. Forward search
Forward search specifies a pattern that must match but is not returned in the result. Forward search is actually a subexpression, which starts with ?=, and the text to be matched follows =. Look at an example of matching the protocol part of a URL address: Text:http://blog.csdn.net/mhmyqn
Regular expression: .+(?=:)
Result:[http]://blog.csdn.net/mhmyqn
Analysis: The protocol part in the URL address is the part before :, the pattern .+ matches any text, the subexpression (?=:) matches:, but the matched: does not appear in the result . We use ?= to indicate to the regular expression engine that as long as: is found, it will be fine, but it will not be included in the final return result. If you do not use forward matching (?=:) here, but use (:) directly, then the matching result will be http:, which includes:, which is not what we want.Note: The front and back in front and back search refer to the relative position between the pattern and the text to be searched. The left is the front and the right is the back. That is, the forward search is: xxx(?=xxx), and the backward search is (?
3. Backward search
Backward searchOperator is?JavaScript does not support it, and the Java language supports backward search.
For example, if you want to find the price in the text (starting with $, followed by a number), the result does not contain the currency symbol: Text:category1:$136.25,category2:$28,category3: $88.60
Regular expression:(?
Result:category1: $【136.25】,category2:$【28】,category3:$【88.60】
Analysis: (?< ;=\$) pattern matches $, \d+(\.\d+)? pattern matches integer or decimal. As can be seen from the results, the result does not include the currency symbol, but only the price. What would happen if we didn't use backward lookup? Use the pattern $\d+(\.\d+)?, which will include $ in the result. Using the pattern \d+(\.\d+)? will also match the numbers in category1(23), which is not what we want.Note: The length of the forward search pattern is variable, and they can contain metacharacters such as ., *, +; while the backward search pattern can only be of fixed length and cannot contain. , *, + and other metacharacters.
4. Combine forward search and backward search
Use forward search and backward search together to solve the previous# Problem with text between ##HTML tagsRegular expression: (?).*?(?= [Tt][Ii] [Tt][Ll][Ee]>) Result: Analysis: It can be seen from the results that the problem is perfectly solved. (?) is a backward operation, which matches
5. Find the difference between the forward and backward searches
The forward search and backward search mentioned above are usually used to match text, and their purpose is to determine whether The position of the text of the matched result that is returned (by specifying what text must be before and after the matched result). This usage is called forward search and forward search. There is also a negative forward search and a negative backward search, which find text that does not match a given pattern.
Operators for searching before and after:
(?=) | Correct Forward search |
(?!) | Negative forward search |
I paid $30 for 10 apples , 15 oranges, and 10 pears. I saved $5 on this order.
Regular expression:(?
Result :I paid 【$30】 for 10 apples, 15 oranges, and 10 pears. I saved 【$5】 on this order.
Find quantity:
Text:I paid $30 for 10 apples, 15 oranges, and 10 pears. I saved $5 on this order.
Regular expression:\b(?
Result:I paid $30 for 【10】 apples, 【15】 oranges, and 【10】pears. I saved $5 on this order.
Analysis: (?< ;!\$) represents a negative lookbehind search, which causes the result to contain only those values that do not begin with $.
6. Summary
With forward and backward search, you can have precise control over what content is included in the final matching result. The before and after search operation allows us to use subexpressions to specify the location where the text matching operation occurs, and achieve the effect of only matching without consumption.PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools.jb51.net/regex/javascript
Regular expression online generation tool:
http://tools.jb51.net/regex/create_reg
The above is the detailed content of Detailed explanation of lookaround before and after regular expression tutorial_regular expression. For more information, please follow other related articles on the PHP Chinese website!

关闭iPhone版“查找”后会发生什么?“查找我的iPhone”可帮助您定位丢失或被盗的设备。启用后,“查找我的iPhone”可让您在地图上跟踪设备的位置、播放声音并帮助您找回设备。“查找”还包括一个激活锁,可防止任何人使用您的iPhone。当您关闭“查找我的iPhone”时,您将失去所有这些功能,这可能会使恢复丢失的Apple设备变得困难。虽然“查找我的iPhone”非常有用,但当您想出售、捐赠、以旧换新手机或想要将其送去更换电池或任何其他服务时,您应该禁用它。这样做将确保没有人可以访问有关您

使用C#中的Array.IndexOf函数查找数组中某个元素的索引在C#程序中,当我们需要查找数组中某个元素的索引时,可以使用Array.IndexOf函数。Array.IndexOf函数会在指定的数组范围内查找指定的元素,并返回其第一次出现的索引。如果未找到该元素,则返回-1。下面是一段示例代码,演示了如何使用Array.IndexOf函数查找数组中某个元

在这个问题中,我们得到一个包含n个未排序整数值的数组aar[]和一个整数val。我们的任务是在未排序的数组中查找元素的开始和结束索引。对于数组中元素的出现,我们将返回,“起始索引和结束索引”(如果在数组中找到两次或多次)。“单个索引”(如果找到)如果数组中不存在,则“元素不存在”。让我们举个例子来理解问题,示例1Input:arr[]={2,1,5,4,6,2,3},val=2Output:startingindex=0,endingindex=5解释元素2出现两次,第一次出现在索引=0处,第二

如何用Python编写哈希查找算法?哈希查找算法,又称为散列查找算法,是一种基于哈希表的数据查找方法。相比于线性查找和二分查找等传统查找算法,哈希查找算法具有更高的查找效率。在Python中,我们可以使用字典(dictionary)来实现哈希表,进而实现哈希查找。哈希查找算法的基本思想是将待查找的关键字通过哈希函数转换成一个索引值,然后根据索引值在哈希表中查

Collection是一个接口,而Collections是Java中的一个实用程序类。Set、List、和Queue是Collection接口的一些子接口,Map接口也是一部分Collections框架的一部分,但它不继承Collection接口。Collection接口的重要方法有add()、remove()、size()、clear()等,并且Collections类仅包含静态方法,如sort()、min()、max()、fill()、copy()、reverse()等。集合接口的语法pub

在这个问题中,我们得到一个由N个数字和一个整数值x组成的数组arr[]。我们的任务是创建一个程序,使用二进制提升在N个数字的前缀和中查找大于或等于X的第一个元素。前缀和是一个数组,其每个元素是初始数组中直到该索引为止的所有元素的总和。示例-array[]={5,2,9,4,1}prefixSumArray[]={5,7,16,20,21}让我们举个例子来理解这个问题,Input:arr[]={5,2,9,4,1},X=19Output:3解决方案在这里,我们将使用二元提升的概念来解决问题。二元提

如何在PHP数组中查找特定值在PHP编程中,经常需要在数组中查找特定值,以实现各种功能。本文将介绍几种常见的方法,帮助读者理解如何在PHP数组中高效地查找特定值。一、线性查找法线性查找法是最基本的查找方法,适用于无序数组。它通过逐个比较数组元素,找到目标值后返回其位置。functionlinearSearch($arr,$target){$n

数组的目的是将相似类型的数据存储在一系列可以使用基地址和索引访问的内存位置中。我们在许多不同的应用程序中使用数组来保存用于各种目的的数据。查找最小和最大元素是数组的一个相当常见的示例,在包括排序等在内的多个应用程序中都需要数组。在本文中,我们将了解如何在C++中从数组中查找第二大元素。通过示例理解概念GivenarrayA=[89,12,32,74,14,69,45,12,99,85,63,32]Thesecondlargestelementis89在上面的示例中,数组中有12个元素。数组中最大


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Dreamweaver Mac version
Visual web development tools
