0

I have the string 562865_numbersletterssymbols

If I want to delete, IF EXISTS; the first part (562865_), what should I search?

My guess was ^[^?:(562865_)]+$ to take what was not "562865_" (if existing ?:) until $ +$

But I discovered that (562865_) searchs every single digit and not the whole string.

How can I find the solution?

5
  • What language are you using? Why use Regex for this? Commented Dec 27, 2012 at 18:42
  • are you looking specifically for the exact sequence "562865_" or any set of numbers, followed by an underscore? Commented Dec 27, 2012 at 18:43
  • I'm not familiar with php, but why not find "562865_" in the target string, and then replace the match(if any) with an empty string? I would prefer this approach to finding what's "not" 562865_ Commented Dec 27, 2012 at 18:46
  • 4
    $your_var = str_replace('562865_', '', $your_var), no? Sorry for not posting this as an answer, but I can't believe in this question being extremely localized. Commented Dec 27, 2012 at 18:46
  • I can't believe I am that idiot, thanks. Commented Dec 27, 2012 at 18:56

3 Answers 3

1

The easiest way that I was able to come up with is this. It will work not only for that name but any others with the same format.

<?php
$string = "562865_numbersletterssymbols";

echo preg_replace("/^\d+_/", "", $string);
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to stick with regex, try ^[\d]+_(.+)$

the _ splits the string into 2 parts, and you need the second part in parenthesis (.+)

3 Comments

[\d] - why you put a character class into a character class?
I'd have problems if I don't find that exact initial part, because it will strip away a lot more of the string, till the first "_"
@user1722791 if your initial part is always 562865_, how about ^562865_(.+)$ ?
0

Thanks to raina77ow, the easiest way is always the best choice.

$your_var = str_replace('562865_', '', $your_var)

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.