2

PHP's preg_replace() function is not replacing all lines in:

12*some input
12*some input
1*some input

The code is:

preg_replace("/^(\d{1,2}[^0-9])/", "", $text);

The result is:

some input
12*some input
1*some input

But I want this:

some input
some input
some input
2
  • As it is a programming question this would be better suited to be posted on stackoverflow as coding is considered off-topic on superuser.
    – Seth
    May 9, 2017 at 10:39
  • ya know but problem is who is dont know ask Question clear or spell mistake or no English knowledge or entry people all stackoverflow 1 or 2 Qus after cant ask!
    – A. Sang
    May 9, 2017 at 18:01

1 Answer 1

1

Depending on how the implementation of PHP does it, I think you're missing either an option or your regex doesn't do what you think it does.

/^(\d{1,2}[^0-9])/

The above regex would look for 1-2 numbers followed not by numbers from the start of the string. Depending on how it works a line break doesn't indicate that ^ should match again.

If you look at the PCRE Pattern Modifiers in the manual you likely need to supply the m flag to turn on multi line mode.

In addition, though it's missing from that manual page, you might need the global flag. So the above regex would become:

/^(\d{1,2}[^0-9])/gm

You might also be able to test this regular expression on platforms like RegEx 101.

2
  • From OP A. Sang: yes finally works: /^(\d{1,2}[^0-9])/m
    – fixer1234
    May 10, 2017 at 16:47
  • 3
    I don't think (not 100% sure) the /g modifier is valid in PHP (in contrast to Perl). Jul 4, 2019 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .