Regular expression: How to match string without the last digits? - regex

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];

Related

Regex position string

For example i have this string.
$string = 'test***bas';
How can I display text before the stars with Regex?
You could use a regular expression which makes use of Capture Groups. Once that you have matched your input, you could then access the captured group and print the output.
The following pattern
^(.+?)\*\*\*
will create a group match using the parenthesis operators. See http://gskinner.com/RegExr/ for testing your regular expressions (there are many ways of testing online)
The language you use around your regular expression will have different ways of capturing groups so you will need to better explain what language you are using for any further advice.
Example for before and after asterix
^(.+?)\*\*\*(.+)$
If tou also want what is located after the ***, you can use the following:
$string = 'test***bas';
$pattern = '/(.+)\*{3}(.+)/';
preg_match($pattern, $string, $matches);
$matches will contain the results:
$matches[1] will be "test"
$matches[2] will be "bas"

extract substring using regex

I would like to extract the string between "t=" and '&' from the string below, but '&2384' may not be present sometimes. I have tried the following, but I get the result "123455asdfgh&2384" instead of "123455asdfgh", what am I doing wrong here? thanks for help.
$string="t=123455asdfgh&2384";
$match=array();
preg_match('/t=(.*)(&.*)?/', $string, $match);
echo $match[1];
NOTE: I need to use regular exp...
Try this one instead
preg_match('/t=([^&]*)(&.*)?/', $string, $match);
This is better suited for parse_str(), and not a regex.
parse_str( "t=123455asdfgh&2384", $params);
echo $params['t'];
This prints:
123455asdfgh
This is what parse_str is for (parsing query strings):
$string="t=123455asdfgh&2384";
$args = array();
parse_str($string, $args);
echo $args['t']; // outputs '123455asdfgh'
Try a non greedy quantifier:
preg_match('/t=(.*?)(?=&|$)/', $string, $match);
The problem is that the first .* matches everything up till the end of the string, which is still a complete match because the latter group is optional.
How about using the strpos() to find the starting and ending point in the string, and then extract it?

Close last 4 characters in breaket of php string

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)

Need Regex to parse String

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");

Get numbers from string with regex

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