I have some strings like below
my-name-is-2547
this-is-stack-2012
hllo-how-2011
Now I want the above strings to be changed to something like the ones below using regex.
my-name-is-(2547)
this-is-stack-(2012)
hllo-how-(2011)
I don't want to use substr or other, only regex replace.
$pattern = '/(\d+)$/';
$replacement = '($1)';
echo preg_replace($pattern, $replacement, $string);
If you are sure that a numbers are only at the end:
regular expression:
(\d+)
using 1 capturing group. Replaced by: ($1).
so the outpu will be:
my-name-is-(2547)
this-is-stack-(2012)
hllo-how-(2011)
Related
Is there a possibility to use Regex for finding matching string between two dots?
I have strings with direcotries and I need to find string between two dots. Eg:
$string = '/Folder/file.co.txt';
and regex will return ONLY co between two dots.
I've tried following pattern: '/..../', but it returned .co. with dots.
Is there a possibility to do this with regex or all I can do is splice returned string?
You could use lookaround:
$string = '/Folder/file.co.txt';
preg_match('/(?<=\.)..(?=\.)/', $string, $matches);
echo $matches[0];
Output:
co
If you use preg_match you can set braces like () to define a group.
Your Statement can look like
"~\.(..)\.~g"
I am using the code as follows,
Code:
my $str = 123455;
if ($str =~ m/([a-z]+)|(\d+)/ {
print "$1\n";
}
I know that it will not print the result because we should give $2. But I want to get the result as it is using the same code by changing the regular expression.
Is it possible to do it?
Note :
Please do not provide the result as below,
my $str = 123455;
if ($str =~ m/(?:[a-z]+)|(\d+)/ {
print "$1\n";
}
You can use (?| .. ) for alternative capture group numbering,
use 5.010; # regex feature available since perl 5.10
my $str = 123455;
if ($str =~ m/(?| ([a-z]+)|(\d+) )/x) {
print "$1\n";
}
([a-z]+|\d+)
Try this.Replace by $1.See demo.
http://regex101.com/r/sZ2wJ5/1
Add anchors if you want to match only letters or numbers at a time.
^([a-z]+|\d+)$
or
((?:[a-z]+)|(?:\d+))
You could use print "$&\n".
$& contains the entire matched string (in other words : either $1 or $2).
See http://perldoc.perl.org/perlre.html for more details ;-)
What do you mean you don't want to change your group structuring? You want your capture to go to group 1, but what you have won't ever put a number in group 1. You have to change your group structuring.
If you still want to be able to find a numeric in group 2, you can create subgroups -- groups number from the opening parenthesis. Try
([a-z]+|(\d+))
if that's what you want.
This is my string: left_image_12.
I would like to leave out _12 and only display left_image. I am unable to figure this out.
You can do that with a capture group:
^(.*?)_\d+$
$1 (or \1, depending on your language) will contain the name without the number at the end.
Try this:
preg_match($string, "/^([A-Za-z_]+)_\d+$/", $match);
$output = $match[1];
Using a regular expression, I want to parse a String like "DOCID = 1234567 THIS IS TEST" and remove the remaining String after the numbers.
How can I do this using the VIM editor?
In Perl:
$str =~ s/(= \d+).*$/$1/;
In php:
$str = preg_replace('/(= \d+).*$/', "$1", $str);
That will do the job:
:%s/\d\+\zs.*
Explanation:
% use the whole buffer, you can omit this if you want to change current line only
s the substitute command
\d\+ match as many numbers
\zs set the start of match here
.* everything else
you can omit the replacement string because you want to delete the match
In VIM, in command mode (press ESC), write :
:s/\([^0-9]\+[0-9]\+\).*/\1/
This will do the job.
If you want to do all replacement possible, then :
:s/\([^0-9]\+[0-9]\+\).*/\1/g
in java string.replaceFirst("(= \\d+).*$","\\1");
I am trying to write a regex to get the numbers from strings like these ones:
javascript:ShowPage('6009',null,null,null,null,null,null,null)
javascript:BlockLink('2146',null,null,null)
I am having difficulty writing the regex to grab these numbers.
How should I do this?
Try this:
(\d+)
What language are you using to parse these strings?
If you let me know I can help you with the code you would need to use this regular expression.
Assuming:
you want to capture the digits
there's only one set of digits per line
Try this:
/(\d+)/
then $1 (Perl) or $matches[1] (PHP) or whatever your poison of choice is, should contain the digits.
Integer or float:
/\d+((.|,)\d+)?/
just match numbers: \d+
// PHP
$string = 'ssss 12.2';
$pattern = '/\D*(\d+)(.|,)?(\d+)?\D*/';
$replacement = '$1.$3';
$res = (float)preg_replace($pattern, $replacement, $string);
// output 12.2