From the below line:
[1;32mget[0m /dir/dir/dir {} [oiid:197,uiid:7522] [32m200[0m (676ms)
convert it to:
get /dir/dir/dir {} [oiid:197,uiid:7522] 200 (676ms)
any suggestions on regex please the value 200 can be any number and the value get can be any http method.
I have tried
$_.replace("[[\d+m]","").replace('[[1;\d+m]',"").replace('[[\d+]m]',"")
} | Set-Content $newfilepath```
You can do the following using -replace operator and a regex:
$string = '[1;32mget[0m /dir/dir/dir {} [oiid:197,uiid:7522] [32m200[0m (676ms)'
$string -replace '\[\d.*?m'
\[ matches [ and note that it needs to be escaped for literal match because [ is special to regex. \d is a digit. .*? matches as few characters as possible until m is matched.
The String class Replace() method does not support regex. So you cannot use regex expressions like \d inside.
Related
i want to remove every string starts with one or more \ and follow with n.
For example:
input: {\n\n abc \\nb\\\ncc}
expect output: { abc bcc}
In javascript it works with regex /\\+n/g
But it doesn't work in swift:
str.replacingOccurrences(of: "\\+n", with: "", options: .regularExpression)
To remove all ns that contain one or more backslashes in front, and all newlines with any amount of backslashes in front, you can use
#"\\+n|\\*\n"#
Note the # before and after the double quotes mean that the literal is a raw string literal where backslashes are treated as literal backslashes and do not form string escape sequences, like \n, \t, \r, etc.
Here, the pattern means
\\+n - one or more backslashes followed with n
| - or
\\*\n - zero or more backslashes followed with a newline.
Note that here, \n is a regex escape matching a newline, it is not an LF, \x0A character.
Been scratching my head on this one...
I'd like to remove .com and capitalize S and T from: "sometext.com"
So output would be Some Text
Thank you in advance
For most of this you can use the replace() member of the String object.
The syntax is:
$string = $string.replace('what you want replaced', 'what you will replace it with')
Replace can be used to erase things by using blank quotes '' for the second argument. That's how you can get rid of .com
$string = $string.replace('.com','')
It can also be used to insert things. You can insert a space between some and text like this:
$string = $string.replace('et', 'e t')
Note that using replace does NOT change the original variable. The command below will print "that" to your screen, but the value of $string will still be "this"
$string = 'this'
$string.replace('this', 'that')
You have to set the variable to the new value with =
$string = "this"
$string = $string.replace("this", "that")
This command will change the value of $string to that.
The tricky part here comes in changing the first t to capital T without changing the last t. With strings, replace() replaces every instance of the text.
$string = "text"
$string = $string.replace('t', 'T')
This will set $string to TexT. To get around this, you can use Regex. Regex is a complex topic. Here just know that Regex objects look like strings, but their replace method works a little differently. You can add a number as a third argument to specify how many items to replace
$string = "aaaaaa"
[Regex]$reggie = 'a'
$string = $reggie.replace($string,'a',3)
This code sets $string to AAAaaa.
So here's the final code to change sometext.com to Some Text.
$string = 'sometext.com'
#Use replace() to remove text.
$string = $string.Replace('.com','')
#Use replace() to change text
$string = $string.Replace('s','S')
#Use replace() to insert text.
$string = $string.Replace('et', 'e t')
#Use a Regex object to replace the first instance of a string.
[regex]$pattern = 't'
$string = $pattern.Replace($string, 'T', 1)
What you're trying to achieve isn't well-defined, but here's a concise PowerShell Core solution:
PsCore> 'sometext.com' -replace '\.com$' -replace '^s|t(?!$)', { $_.Value.ToUpper() }
SomeText
-replace '\.com$' removes a literal trailing .com from your input string.
-replace '^s|t(?!$), { ... } matches an s char. at the start (^), and a t that is not (!) at the end ($); (?!...) is a so-called negative look-ahead assertion that looks ahead in the input string without including what it finds in the overall match.
Script block { $_.Value.ToUpper() } is called for each match, and converts the match to uppercase.
-replace (a.k.a -ireplace) is case-INsensitive by default; use -creplace for case-SENSITIVE replacements.
For more information about PowerShell's -replace operator see this answer.
Passing a script block ({ ... }) to dynamically determine the replacement string isn't supported in Windows PowerShell, so a Windows PowerShell solution requires direct use of the .NET [regex] class:
WinPs> [regex]::Replace('sometext.com' -replace '\.com$', '^s|t(?!$)', { param($m) $m.Value.ToUpper() })
SomeText
If I use the Trim() method on a string containing -char-$-repeated char-, e.g. "BL$LA" or "LA$AB", Trim() strips the repeated char after the $ as well.
For example:
$a = 'BL$LA'
$b = $a.Trim("BL$")
returns A not LA, but
$a = 'BM$LA'
$b = $a.Trim("BM$")
returns LA.
Any reason why? Or am I missing something?
The Trim() method removes all characters in the given argument (the string is automatically cast to a character array) from beginning and end of the string object. Your second example only seems to be doing what you want, because the remainder of the string does not have any of the characters-to-be-trimmed in it.
Demonstration:
PS C:\> $a = 'BL$LA'
PS C:\> $a.Trim("BL$")
A
PS C:\> $a = 'LxB$LA'
PS C:\> $a.Trim("BL$")
xB$LA
To remove a given substring from beginning and end of a string you need something like this instead:
$a -replace '^BL\$|BL\$$'
Regular expression breakdown:
^ matches the beginning of a string.
$ matches the end of a string.
BL\$ matches the literal character sequence "BL$".
...|... is an alternation (match any of these (sub)expressions).
If you just want to remove text up to and including the first $ from the beginning of a string you could also do something like this:
$a -replace '^.*?\$'
Regular expression breakdown:
^ matches the beginning of a string.
\$ matches a literal $ character.
.*? matches all characters up to the next (sub)expression (shortest/non-greedy match).
E.g:
$myVar="this###!~`%^&*()[]}{;'".,<>?/\";
I am not able to export this variable and use it as it is in my program.
Use q to store the characters and use the quotemeta to escape the all character
my $myVar=q("this###!~`%^&*()[]}{;'".,<>?/\");
$myVar = quotemeta($myVar);
print $myVar;
Or else use regex substitution to escape the all character
my $myVar=q("this###!~`%^&*()[]}{;'".,<>?/\");
$myVar =~s/(\W)/\\$1/g;
print $myVar;
This is what quotemeta is for, if I understand your quest
Returns the value of EXPR with all non-"word" characters backslashed. (That is, all characters not matching /[A-Za-z_0-9]/ will be preceded by a backslash in the returned string, regardless of any locale settings.) This is the internal function implementing the \Q escape in double-quoted strings.
Its use is very simple
my $myVar = q(this###!~`%^&*()[]}{;'".,<>?/\\);
print "$myVar\n";
my $quoted_var = quotemeta $myVar;
print "$quoted_var\n";
Note that we must manually escape the last backslash, to prevent it from escaping the closing delimiter. Or you can tack on an extra space at the end, and then strip it (by chop).
my $myVar = q(this###!~`%^&*()[]}{;'".,<>?/\ );
chop $myVar;
Now transform $myVar like above, using quotemeta.
I take the outside pair of " to merely indicate what you'd like in the variable. But if they are in fact meant to be in the variable then simply put it all inside q(), since then the last character is ". The only problem is a backslash immediately preceding the closing delimiter.
If you need this in a regex context then you use \Q to start and \E to end escaping.
Giving Thanks to:
What's between \Q and \E is treated as normal characters, not regexp characters. For example,
'.' =~ /./; # match
'a' =~ /./; # match
'.' =~ /\Q.\E/; # match
'a' =~ /\Q.\E/; # no match
It doesn't stop variables from being interpolated.
$search = '.';
'.' =~ /$search/; # match
'a' =~ /$search/; # match
'.' =~ /\Q$search\E/; # match
'a' =~ /\Q$search\E/; # no match
I am trying to find a pattern using perl. But I am only interested with the beginning and the end of the pattern. To be more specific I have a sequence of letters and I would like to see if the following pattern exists. There are 23 characters. And I'm only interested in the beginning and the end of the sequence.
For example I would like to extract anything that starts with ab and ends with zt. There is always
So it can be
abaaaaaaaaaaaaaaaaaaazt
So that it detects this match
but not
abaaaaaaaaaaaaaaaaaaazz
So far I tried
if ($line =~ /ab[*]zt/) {
print "found pattern ";
}
thanks
* is a quantifier and meta character. Inside a character class bracket [ .. ] it just means a literal asterisk. You are probably thinking of .* which is a wildcard followed by the quantifier.
Matching entire string, e.g. "abaazt".
/^ab.*zt$/
Note the anchors ^ and $, and the wildcard character . followed by the zero or more * quantifier.
Match substrings inside another string, e.g. "a b abaazt c d"
/\bab\S*zt\b/
Using word boundary \b to denote beginning and end instead of anchors. You can also be more specific:
/(?<!\S)ab\S*zt(?!\S)/
Using a double negation to assert that no non-whitespace characters follow or precede the target text.
It is also possible to use the substr function
if (substr($string, 0, 2) eq "ab" and substr($string, -2) eq "zt")
You mention that the string is 23 characters, and if that is a fixed length, you can get even more specific, for example
/^ab.{19}zt$/
Which matches exactly 19 wildcards. The syntax for the {} quantifier is {min, max}, and any value left blank means infinite, i.e. {1,} is the same as + and {0,} is the same as *, meaning one/zero or more matches (respectively).
Just a * by itself wont match anything (except a literal *), if you want to match anything you need to use .*.
if ($line =~ /^ab.*zt$/) {
print "found pattern ";
}
If you really want to capture the match, wrap the whole pattern in a capture group:
if (my ($string) = $line =~ /^(ab.*zt)$/) {
print "found pattern $string";
}