开发者

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

开发者 https://www.devze.com 2023-01-31 19:54 出处:网络
I\'m try开发者_StackOverflow中文版ing 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?/

I'm try开发者_StackOverflow中文版ing 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?


/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.


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(....)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号