The 5th word from sentence using RegEx [duplicate] - regex

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

Related

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

Use RegEx to find and transform characters to capital case [duplicate]

This question already has answers here:
Notepad++ and regex: how to UPPERCASE specific part of a string / find / replace
(2 answers)
Closed 4 years ago.
In notepad++ I need to use RegEx transform all
phone1_id, phone2_id, phone3_id
in
PHONE1_ID, PHONE2_ID, PHONE3_ID
This RegEx helps me find all those strings: phone\d+_id
but how can I transform them to capital case?
Ctrl+H
Find what: phone\d+_id
Replace with: \U$0
check Wrap around
check Regular expression
Replace all
Replacement:
\U : Change to uppercase
$0 : contains the whole match
Result for given example:
PHONE1_ID, PHONE2_ID, PHONE3_ID

Perl Regular Expression issues [duplicate]

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.

Add spacebar in regular expression [duplicate]

This question already has answers here:
Matching a space in regex
(10 answers)
Closed 9 years ago.
I use this regular expression for checking
public const string FullNameRegularExpression = #"^[a-zA-Z0-9._-]+$";
How to add "spacebar" in?
If you are looking for one single space it is: (" "), a very complete example can be found in this reference.
Or if you want to match any whitespace character (\n,\r,\f,\t, ), you can use \s.
Notice an added \s
public const string FullNameRegularExpression = #"^[a-zA-Z0-9._-\s]+$";
You may push a spacebar on your keyboard or add \s or \s+ or \s* to your regex ;-)