Need Regex to parse String - regex

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

Related

PowerShell Regex with "." PowerShell

i want to replace a part of some Strings in a loop with PowerShell
example string
testvm029.vmxxx
I want to replace everything at .vmxxx. Every string has another length but all ends with .vm... So the result should be: testvm029
I tried the following Script:
Foreach($String in $Strings) {
$StringTest = $String -replace "(.vm)(.+)","$null"
}
Of course this kills my String on the first vm and not at .vm...
result:
test
how can i achieve my goal?
EDIT: .vm is not the last '.' of my String so something like that:
test123.vmabc.string
I want to cut it after .vm
You need to use
$String -replace '\.vm.*'
See the regex demo
The pattern will find the first .vm substring (with \.vm) and then match the rest of the string (with .*). The match will be replaced with an empty string (it is used by default, but you may write it explicitly, $String -replace '\.vm.*', '').

Regular Expression - Perl

I am trying to get the a sub string from a string using regular expression but it getting error as my regular expression is not working. Can any one help me out in writing correct one :
Here is the Pattern on which i am trying to write the regular expression :
MSM8_BD_V4.3_1-1_idle-Kr_Run3.xlsx
MSM8_BD_V4.3_2-6_mp3-Kr_Run2.xlsx
MSM8_BD_V4.3_Camera_snap-7.xlsx
MSM8_BD_V4.3_Camera_snap-8.xlsx
MSM8_BD_V4.3_Radio_202.16-0.xlsx
I am trying to get the bold part of the substring .
below is the Regular expression i tried:
my $line = "MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx";
my ($captured) = $line =~ /MSM8939_BD_V4\.\3\_[d]*(.+?)\w/gx;
print "$captured\n";
[d] matches nothing but the literal letter d. You want \d, without the brackets, to match a digit. However, it looks like you also want to include underscores. That would be [\d_].
Try this:
/^MSM8_BD_V4\.3_[\d_]*-?([^-]+)/
If I run this on your input (with e.g. perl -nE 'say $1 if /^MSM8_BD_V4\.3_[\d_]*-?([^-]+)/'), I get this output:
1_idle
6_mp3
Camera_snap
Camera_snap
Radio_202.16
my $line = "MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx";
for (qw(
MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx
MSM8939_BD_V4.3_2-6_mp3-Kratos_Run2.xlsx
MSM8939_BD_V4.3_Camera_snap-7.xlsx
MSM8939_BD_V4.3_Camera_snap-8.xlsx
MSM8939_BD_V4.3_Radio_202.16-0.xlsx
)) {
my ($captured) = ($_ =~ /.*[-_]([^\W_]+_[\w.]+)-/gx);
print "$captured\n";
}
Use a greedy pattern to go as far as possible, then grab the last two strings that look like what you want which are still followed by a hyphen.
As does the other answer which was just edited while I was typing, this produces:
1_idle
6_mp3
Camera_snap
Camera_snap
Radio_202.16
This one may be more general in that the beginning of the substring is not hard-coded, i.e., you could use it in other cases which did not necessarily start with MSM8_BD_V4.3.

Regular Expression (Regex) for pulling name between quotations - Powershell

I have strings that look as follows:
\\.\ROOT\abc\kjasdkj\MyClass:InstanceName.name="sxs-test3"
I want a regex that can only pull out the name in quotations so the result is sxs-test3
Also, I am using windows Powershell to do this, can this be done in PowerShell?
Thanks
Here's another option that doesn't require regex:
$path.TrimEnd('"').Split('="',[StringSplitOptions]::RemoveEmptyEntries)[-1]
For that string this regex seems simplest to me:
$string = '\\.\ROOT\abc\kjasdkj\MyClass:InstanceName.name="sxs-test3"'
if ($string -match '"(.+?)"') {
$Matches[1]
}
Matches everything inside double quotes as few times as possible (lazy expansion).
if ($subject -cmatch '(?<=")[^"]*(?=")') {
$result = $matches[0]
}
This looks for any number of characters except quotes ([^"]*), but only if they are preceded by a quote ((?<=")) and followed by a quote ((?=")).
It does not even try to handle escaped quotes.

Regular expression which matches a specific pattern

I want to find a regular expression in Perl which matches a pattern such as this:
my $sumthing = "people say
for -->";
Over here after say there is a single newline character. So I need to find a regular expression which could match such a pattern which includes a newline within a pattern. Please help me to find this as I'm new to Perl & regular expression.
The possible methods I tried were these:
if (($sumthing !~ (/\n+$/)) && ($sumthing !~ (/^\n+/m)))
They kindly help me to find out an expression to match this kind of a pattern, but not getting the output as desired.
It's not clear what you want. Do you want match that string exactly? If so, you could use
$sumthing =~ /^people say\nfor -->\z/
or
$sumthing eq "people say\nfor -->"
Or maybe what you need to know is that . matches any character including newline when /s is used?
/people .* -->/s
The following will check for anything then new line then anything. Not sure if I totally understood your question.
if($sumthing =~ m/.*\n.*/)
Have a look at the /s modifier which causes .to match anything, including a newline.
my $str = "people say for\nsomething...";
$str =~ m{say(.*)}s and print "'$1'\n";
This would print:
' for
something...'

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