[Regular Expressions] Find a String that Precedes Another String ?= , ?!

Let‘s image tow cases for the following string:

var str = `foo
foobar
foobaz
fooboo`

First of all: we know how to capture foobar or fooboo:

var regex = /foo(bar|boo)/g

1: We want to capture any ‘foo‘ which followed by ‘bar‘ or ‘boo‘, but we do NOT want ‘bar‘ or ‘boo‘ appear in our result:

So we can use:

?= 

so:

var regex = /foo(?=bar|boo)/g

2. We want to capture any ‘foo‘ without ‘bar‘ or ‘boo‘ followed:

so we can use:

?!

so:

var regex = /foo(?!bar|boo)/g

时间: 2024-08-26 20:14:36

[Regular Expressions] Find a String that Precedes Another String ?= , ?!的相关文章

PCRE Perl Compatible Regular Expressions Learning

catalog 1. PCRE Introduction 2. pcre2api 3. pcre2jit 4. PCRE Programing 1. PCRE Introduction The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own nat

[转]8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

Using Regular Expressions in Python

1. 反斜杠的困扰(The Backslash) 有时候需要匹配的文本带有'\',如'\python',因为正则表达式有些特殊字符有特殊意义,所以需要前面加上'\'来消除特殊意义,这里匹配的正则表达式是'\\python',这时候如果要编译这个正则表达式需要re.compile('\\\\python'),因为在传递字符串的时候python本身就需要用'\\'来表示'\',也就造成了反斜杠的泛滥. 使用前缀'r'可以解决这个问题:’r'之后的'\'只是这个字符本身而没有特殊意义,比如r'\n'表

8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

Finding Comments in Source Code Using Regular Expressions

Many text editors have advanced find (and replace) features. When I’m programming, I like to use an editor with regular expression search and replace. This feature is allows one to find text based on complex patterns rather than based just on literal

Eloquent JavaScript #09# Regular Expressions

索引 Notes js创建正则表达式的两种方式 js正则匹配方式(1) 字符集合 重复匹配 分组(子表达式) js正则匹配方式(2) The Date class 匹配整个字符串 Choice patterns 正则匹配的机制 回溯Backtracking Replace 贪婪匹配Greed 动态构建正则表达式 Search The lastIndex property 遍历匹配项 解析INI文件 国际字符 Excercise Regexp golf Quoting style Numbers

Python re module (regular expressions)

regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 1 r"""Support for regular expressions (RE). 2 3 This module provides regular expression matching operations similar to 4 those found in Perl. It supports both 8-bit and Unicode strings; b

regular expressions

regular expressions 参考博客:https://blog.csdn.net/zhouzhaoxiong1227/article/details/52026323?utm_source=blogxgwz1 1.数字 1)正整数: ^[1-9][0-9]*$ 2)非正整数: ^((-[1-9][0-9]*)|(0))$ 3)负整数:^-[1-9][0-9]*$ 4)整数: ^(0|-?[1-9][0-9]*)$ 5)非负浮点数:^\d+(\.\d+)?$ 2.字母 1)英文字符串:

java中String s="abc"及String s=new String("abc")详解

1.   栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2.   栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器.但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性.另外,栈数据可以共 享,详见第3点.堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,Java的垃圾收集器会自动收走这些不再使用的数据.但缺点是,由于要 在运行时动态分配内存,存取速度较慢. ==是判断