user3413723
user3413723

Reputation: 12213

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. So if I have a string hello world a is a very nice letter, it would match a is a very.

Upvotes: 1

Views: 5390

Answers (3)

Federico Piazza
Federico Piazza

Reputation: 30995

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:

a(.{10})

Working Demo

enter image description here

Upvotes: 6

anubhava
anubhava

Reputation: 785058

You can use this regex:

x.{0,10}

RegEx Demo

  • x - matched literal x
  • .{0,10} matches 0 to 10 characters after x

However for simple task like this it is better to use string functions and avoid regex.

Upvotes: 1

Martijn
Martijn

Reputation: 16103

No need for a regex, just find the occurence of the X, and take 10 chars from there:

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

Upvotes: 4

Related Questions