tags:

views:

216

answers:

9

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
A: 

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);
Chacha102
forgot the closing parenthesis, so I closed them for you.
Anthony Forloney
cheers, but those will both eat everything (e.g. `A. Appendix A`), not just numeric values.
Pekka
A: 

Here you go:

/[0-9]+\./

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

Cheers

Marcos Placona
But 3124. Chapter 3 may be corrupted..
Enrico Carlesso
Actually, any sentence like this one would get corrupted !1.
Romain
Corrupted? What do you mean? It would not
Marcos Placona
There is no *g* modifier in PCRE.
Gumbo
Fair enough, but it sure is better than the marked solution. Removed the /g
Marcos Placona
No, it is not. As pointed out earlier, "123. Chapter 3. The one with the spaghetti" would become " Chapter The one with the spaghetti", which is not the desired effect.
Felix
+4  A: 

Try:

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

Which gives:

php > print_r(preg_replace('/^[0-9]+\. +/', '', '1231241. dfg'));
dfg
Enrico Carlesso
Note that `.` represents one arbitrary character and not the period character.
Gumbo
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
How's this different from?/[0-9]+\./gBesides being greedy and verbose?
Marcos Placona
@mplacona It starts with a ^. Very important.
Felix
All right, fixed with suggestions...
Enrico Carlesso
+3  A: 

Voilà:

^[0-9]+\.
Romain
A: 
print preg_replace('%^(\d+\. )%', '', '1290394958595. Appendix A');
Felix Kling
A: 

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
It should be `preg_replace('/^[0-9]+\. */', '', $myline)`.
Felix Kling
\*blushes\* Thanks - now fixed. :-)
Tim
+1  A: 

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
+1  A: 

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
@Felix cheers, this is very good and a case I hadn't though of myself. +1.
Pekka
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
A: 
$str="1290394958595. Appendix A";
$s = explode(" ",$str,2);
if( $s[0] + 0 == $s[0]){
  print $s[1];
}
ghostdog74