John
John

Reputation: 16058

How can I make a regex match the next 4 characters immediately after finding something?

I'm trying to write a regex to sift through a sizable amount of data. After it finds something, I want it to match the next 4 characters whatever they are. How can I do this?

Upvotes: 2

Views: 11954

Answers (3)

cristian
cristian

Reputation: 8744

Look at this example: I want to match an IP and the next 4 characters after it.I have a regex


(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(.{4})

if you match that against the following string 192.167.45.45xabc the first part (?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) will match the IP and the last part (.{4}) will match xabc. (I had added ?: at the beginning to make the first block noncapturing - if you want to capture the IP to just remove ?:) I hope this helps

Upvotes: 1

Phrogz
Phrogz

Reputation: 303421

/match long stuff here..../

The . in a regex is "Any character." Four of them gets you four characters. You could also do:

/match long stuff here.{4}/

This may depend on what language you are writing your regex in.

Upvotes: 11

Bryan Oakley
Bryan Oakley

Reputation: 386285

The expression .... matches any four characters. Append that to your pattern, and put parenthesis around it so that whatever those characters are will be captured.

For example:

[Hh]ello [Ww]orld(....)

Upvotes: 3

Related Questions