简体   繁体   English

正则表达式-在PHP之后匹配'a'和10个字符

[英]Regex - match 'a' and 10 characters following php

I'm looking for a regex to match a character 'a' and also the ten characters after it, no matter what they are. 我正在寻找一个正则表达式来匹配字符“ a”以及它后面的十个字符,无论它们是什么。 So if I have a string hello world a is a very nice letter , it would match a is a very . 因此,如果我有一个字符串hello world a is a very nice letter ,它将与a is a very匹配。

If you want to match that you can use this regex: 如果要匹配,可以使用此正则表达式:

a.{10}

Working Demo 工作演示

By the way, if you want to get the content of the 10 characters, you can use the capturing groups: 顺便说一句,如果要获取10个字符的内容,可以使用捕获组:

a(.{10})

Working Demo 工作演示

在此处输入图片说明

No need for a regex, just find the occurence of the X, and take 10 chars from there: 不需要正则表达式,只需找到X的出现,然后从那里取10个字符:

echo substr($string, strpos($string,'x'), 10);

String functions are extremely fast, compared to regexes. 与正则表达式相比,字符串函数非常快。 In case of simple string manipulation you should always go for the simple stringfunctions 如果使用简单的字符串操作,则应始终使用简单的字符串函数

You can use this regex: 您可以使用此正则表达式:

x.{0,10}

RegEx Demo 正则演示

  • x - matched literal x x匹配的文字x
  • .{0,10} matches 0 to 10 characters after x .{0,10}匹配x后面的0到10个字符

However for simple task like this it is better to use string functions and avoid regex. 但是,对于像这样的简单任务,最好使用字符串函数并避免使用正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM