I know this might be very easy to some,,
I have a simple string like this @¨0+639172523299 (with characters before a mobile number). My question is, how do i remove all the characters before the plus(+)? What i know is to remove a known character as follows:
$number =~ tr/://d; (if i want to remove a colon)
But here, I want all characters before ‘+’ to be removed.
To remove everything up to and including the first +, you can do:
If you want to keep the +, you can put that into the replacement:
The above says: Match “anything” (the
.*) followed by a+(+is a special character in regular expressions, which is why it needs the backslash escape) and replace it with nothing (or in the above example, replace it with a single+).Note that the above will strip out everything up to the LAST
+in the string, which may not be what you want. If you want to keep strip out everything up to the FIRST+in a string, you can do:or
The difference from the first regular expression being the
[^+]*instead of.*, which means “match any character except a+“.For more information on Perl’s regular expressions, the perldoc perlre manual page is pretty good, as is O’Reilly’s Mastering Regular Expressions book.