2

My HTML is:

line1
line2

line3 59800 line4
line5
line6

My goal is to capture: (25 left characters)59800(25 right characters):

I tried with

/.{1,25}59800.{1,25}/igm

But I only captures:

line3 59800 line4

How do I capture multiple lines?

Here is the test: http://regexr.com/39498

2 Answers 2

7

Instead of m, use the s (DOTALL) flag in your regex:

/.{1,25}59800.{1,25}/s

The s modifier is used to make DOT match newlines as well where m is used to make anchors ^ and $ match in each line of multiline input.

Sign up to request clarification or add additional context in comments.

Comments

1

Adding to @anubhava's answer, which is correct:

  • You don't need the i flag as there are no letters mentioned in the regex
  • The g flag doesn't exist in PHP.
  • You can also use (?s) to activate DOTALL mode, allowing the dot to match across lines

In PHP you can do this:

$regex = '~(?s).{1,25}59800.{1,25}~';
if (preg_match($regex, $yourstring, $m)) {
    $thematch = $m[0];
}
else { // No match...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.