Perl Regular Expression issues [duplicate] - regex

This question already has answers here:
In Perl, how can I get the matched substring from a regex?
(6 answers)
Extract the required substring from another string -Perl
(4 answers)
How do I access captured substrings after a successful regex match in Perl?
(4 answers)
Closed 5 years ago.
how do I capture everything after T and everything before T using a regex.
What I have so far is only giving me the number 1.
my $string = '2014-06-09T01:59:54.998Z';
my $mystring = $string =~ m/T(.*)Z/;
I am not very well with regex. I assumed this is getting anything between T and Z. Tried leaving off the Z still prints 1.But it only prints
1

my ($date, $time) = split /T/, $string;
In your case, you forget to put your match in list context.
my ($mystring) = ($string =~ m/T(.*)Z/);
In scalar context, you get the number of captured substrings.

Related

How to exclude a substring in a regular expression? [duplicate]

This question already has answers here:
What is the difference between .*? and .* regular expressions?
(3 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 5 months ago.
There is a line of text:
Lorem ~Ipsum~ is simply ~dummy~ text ~of~ the printing...
To find all the words enclosed in ~~ I use
re.search(r'~([^~]*)~', text)
Let's say it became necessary to use ~~ instead of ~
([^\~]+) indicates to exclude the ~ character from the text within those characters
How do I make a regular expression to exclude a string of characters instead of just one?
That is, ~~Lor~em~~ should return Lor~em
The symbol of the new string must not be excluded and the length of the found string cannot be 0
Use a non-greedy quantifier instead of a negated character set.
re.search(r'~~(.*?)~~', text, flags=re.DOTALL)
re.DOTALL makes . match newline characters.

How can I create a regex pattern than spans multiple lines [duplicate]

This question already has an answer here:
split one line regex in a multiline regexp in perl
(1 answer)
Closed 2 years ago.
I have a long regex pattern that I want to split over multiple lines.
Is there a way to take
$s !~ m"((\d)|(\d))";
and split it so you get something like
$s !~ m"((\d)|"+
"(\d))";
The above guess is incorrect and results in
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE (\d)|/ at perl5.pl line 20.
What can I use?
You can't split a pattern into two like that. You can however have a single pattern spread neatly over multiple lines by using the /x modifier to tell the regex parser to ignore whitespace. For example:
$s !~ m{
((\d)
|
(\d))
}x;

Sed syntax to extract all of text BEFORE last delimiter? [duplicate]

This question already has answers here:
Regular Expression, remove everything after last forward slash
(5 answers)
Closed 3 years ago.
I am trying to get the syntax right so that I can make scanning-client-container-0.2.tar look like scanning-client-container
I am using the delimiter " - " like so:
sed -e 's/-[^*]*$//'
with the result scanning, which is cut off too early
You can use a negated character class in your regex:
sed 's/-[^-]*$//' <<< 'scanning-client-container-0.2.tar'
scanning-client-container
RegEx Details:
-: Match a -
[^-]*: Match 0 or more characters that are not -
$: Match end

Changing the results of a regex capture group to lowercase in powershell [duplicate]

This question already has answers here:
PowerShell Replace number in string
(3 answers)
Closed 3 years ago.
I have this regex capture group:
$lowerPattern='(href[\s]?=[\s]?\"[^"]*[^"]*\")'
which is returning all the matches I need just fine. However I need to replace the capture group with the results all lowercase:
$lowerPatternReplace = '$1'.ToLower()
This doesn't seem to be working. How you lowercase a capture group in powershell regex?
This code seems to work for me. It's just a bit less shorthand. I didn't see a way to do it with backreferences, due to the order of execution (you're lowering the literal string '$1').
$Entry = 'asdHREFasd'
$RegEx = '(href)'
$match = $Entry -match $RegEx
[string]$upper = $Matches[1] #first capture group
[string]$lower = $upper.ToLower()
[string]$Entry.replace($upper,$lower)
source

The 5th word from sentence using RegEx [duplicate]

This question already has answers here:
replacing word after multiple spaces
(6 answers)
Closed 9 years ago.
I have a problem. I want to get the word trust from this sentence and replace it with md5 using RegEx:
host all all 127.0.0.1/32 trust
Find : ^((?:\S+\s+){4})\S+
replace by : ${1}md5
For example, in Perl, i'd do:
my $str = 'host all all 127.0.0.1/32 trust';
$str =~ s/^((?:\S+\s+){4})\S+/${1}md5/;
This regex captures the fifth word: ^.+\s+.+\s+.+\s+.+\s+([^\s]+)\*\* and requires that two asterisks came after the word (and the asterisks are not captured)
You can also have a look at this regex:
^(?:.+?\s+){4}(\b.+?\b)
Replace using this regex to md5 will do the job