5

Would somebody care to help me out with a regex to reliably recognize and remove any number, followed by a dot, in the beginning of a string? So that

1. Introduction

becomes

Introduction

and

1290394958595. Appendix A

becomes

Appendix A
Pekka
  • 442,112
  • 142
  • 972
  • 1,088

9 Answers9

11

Try:

preg_replace('/^[0-9]+\. +/', '', $string);

Which gives:

php > print_r(preg_replace('/^[0-9]+\. +/', '', '1231241. dfg'));
dfg
Enrico Carlesso
  • 6,818
  • 4
  • 34
  • 42
  • 2
    Note that `.` represents one arbitrary character and not the period character. – Gumbo Mar 07 '10 at 23:05
  • 2
    Two things wrong with this one that aren't a problem in @Romain's answer: `[0-9]*` will match zero digits, which is bad - use a `+` instead of the `*`, and the `.` is a special character which means "match any character", and should be escaped by preceding it with a backslash (`\`). – Samir Talwar Mar 07 '10 at 23:06
  • How's this different from? /[0-9]+\./g Besides being greedy and verbose? – Marcos Placona Mar 07 '10 at 23:07
  • 1
    @mplacona It starts with a ^. Very important. – Felix Mar 07 '10 at 23:20
9

I know the question is closed, just my two cents:

preg_replace("/^[0-9\\.\\s]+/", "", "1234. Appendix A");

Would work best, in my opinion, mainly because It will also handle cases such as

1.2 This is a level-two heading
Felix
  • 88,392
  • 43
  • 149
  • 167
  • @Felix cheers, this is very good and a case I hadn't though of myself. +1. – Pekka Mar 07 '10 at 23:47
  • If you consider this answer to be better than the currently accepted one, you can switch it. I'm not trying to steal anyone's thunder, I'm just thinking about anyone with the same problem ending up on this page and not seeing the appropriate answer. – Felix Mar 08 '10 at 10:43
3

Voilà:

^[0-9]+\.
Romain
  • 12,679
  • 3
  • 41
  • 54
1

Ok, this does not qualify for recognize and remove any number, followed by a dot, but it will return the desired string, e.g. Appendix A, so it might qualify as an alternative.

// remove everything before first space
echo trim(strstr('1290394958595. Appendix A', ' '));

// remove all numbers and dot and space from the left side of string
echo ltrim('1290394958595. Appendix A', '0123456789. ');

Just disregard it, it it's not an option.

Gordon
  • 312,688
  • 75
  • 539
  • 559
0

Stuff after string

$string = "1290394958595. Appendix A";
$first_space_position = strpos(" ", $string);
$stuff_after_space = substr($string, $first_space_position);

Stuff after dot

$string = "1290394958595. Appendix A";
$first_dot_position = strpos(".", $string);
$stuff_after_dot = substr($string, $first_dot_position);
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
0

Here you go:

/[0-9]+\./

Btw, I'd definitely use a regular expression here as they are very reliable and lightweight.

Cheers

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
0
print preg_replace('%^(\d+\. )%', '', '1290394958595. Appendix A');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

The PHP function to search a string for a regular expression and to replace that with something else is preg_replace. In this case you want something like:

$mytitle = preg_replace('/^[0-9]+\. */', '', $myline);
Tim
  • 9,171
  • 33
  • 51
0
$str="1290394958595. Appendix A";
$s = explode(" ",$str,2);
if( $s[0] + 0 == $s[0]){
  print $s[1];
}
ghostdog74
  • 327,991
  • 56
  • 259
  • 343