This question already has answers here:
What's the difference between .replace and -replace in powershell?
(2 answers)
Closed 4 years ago.
Edit: though the question above is related, this isn't the same question as asking the difference between .replace and -replace, nor does it have the same answer.
Per the Powershell docs
\d matches any digit character.
I have a command (gg, an alias for git grep) that gives the output:
packages/somemodule/index.js:69: log(`woo`)
I'm familiar with regexs, and would like to change the output to :
packages/somemodule/index.js:69 log(`woo`)
I.e. adding a space after the first digits and the colon (if you're interested, this is to make the file openable by an editor). However a digit, one or more times, followed by a colon \d+: doesn't work:
gg 'No previous' | % {$_.replace("\d+:",'xxxx')}
Trying different versions, the \d doesn't work. What am I doing wrong?
Command output is treated as string data. In your code you are calling the [String].Replace() method which does not support regular expressions. For this to work as expected, you need to use PowerShell's -replace operator.
gg 'No previous' | % { $_ -replace '\d+:','xxxx' }
This approach will allow PowerShell to utilize regular expressions for string replacement!
Related
This question already has answers here:
How to extract text from a string using sed?
(5 answers)
Closed 5 years ago.
I am trying to use \d in regex in sed but it doesn't work:
sed -re 's/\d+//g'
But this is working:
sed -re 's/[0-9]+//g'
\d is a switch not a regular expression macro. If you want to use some predefined "constant" instead of [0-9] expression just try run this code:
s/[[:digit:]]+//g
There is no such special character group in sed. You will have to use [0-9].
In GNU sed, \d introduces a decimal character code of one to three digits in the range 0-255.
As indicated in this comment.
You'd better use the Extended pattern in sed by adding -E.
In basic RegExp, \d and some others won't be detected
-E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats.
This question already has answers here:
JavaScript regex get all matches in a string
(2 answers)
Closed 2 years ago.
I'm trying to build a regular expression that captures anything between square brackets like the following numbers.
[phone]010101[/phone] [phone]434343[/phone]
[phone]3443434[/phone]
so the matches should be 010101, 434343, 3443434
I built cow([\s\S]*?)milk to experiment, and this seems to capture multiple matches and works fine with multiple lines, achieving what I exactly need.
However when I attempted to build the actual regex using this: \[phone\]([\s\S]*?)\[\/phone\] , it would only capture the first single match.
What could be wrong with my expression?
Another approach. This will capture the numbers as you intend.
\](.*)\[
RegexDemo
The regex is correct but global and multi-line flags are missing. In JavaScript, with g (global) and m (multiline) flags added to regex, intended matches can be found.
str=`[phone]010101[/phone] [phone]434343[/phone]
[phone]3443434[/phone]`;
reg = /\[phone\]([\s\S]*?)\[\/phone\]/gm;
[...str.matchAll(reg)].map(x=> x[1]); //["010101", "434343", "3443434"]
This question already has an answer here:
grep regex whitespace behavior
(1 answer)
Closed 4 years ago.
Why does the first grep statement below fail to return results, but the modified grep statement below that works? I have tried egrep as well with same results.
cat test
ALL: 192.168.0.0/255.255.0.0, 10.0.0.0/255.0.0.0
grep '^[\s]*ALL[\s]*:[\s]*192.168.0.0/255.255.0.0[\s]*' test
No results
grep '^[\s]*ALL[\s]*: 192.168.0.0/255.255.0.0[\s]*' test
ALL: 192.168.0.0/255.255.0.0, 10.0.0.0/255.0.0.0
Also , when I put a $ at the end, both fail.
grep '^[\s]*ALL[\s]*:[\s]*192.168.0.0/255.255.0.0[\s]*$' test
No results
grep '^[\s]*ALL[\s]*: 192.168.0.0/255.255.0.0[\s]*$' test
No results
grep is guaranteed to implement BRE -- POSIX basic regular expressions. \s is not meaningful in BRE. (Some OS vendors extend the standard, some don't).
Use [[:space:]] instead to have something that works everywhere.
Adding $ to the end of your expression makes it fail because it matches the end of the line. Your line has an extra , 10.0.0.0/255.0.0.0 after the matching portion, so of course that doesn't match $. You could say .*$, but that would be redundant unless you had the -o/--only-matching flag enabled.
This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 4 years ago.
I'm attempting to replace the beginning of lines in a simple file.
>1
TGAACCATCGAGTCTTTGAACG
>2
GAGTTCATTTCTCTCTGGAGGCACC
>3
ATTGACAGATTGAGAGCTCTTTC
>4
CGGGAAAAGGATTGGCTC
>5
TCTTGGTGGTAGTAGCAAATATTCAAATG
Above is the input, below is the desired output.
>seq_x1
TGAACCATCGAGTCTTTGAACG
>seq_x2
GAGTTCATTTCTCTCTGGAGGCACC
>seq_x3
ATTGACAGATTGAGAGCTCTTTC
>seq_x4
CGGGAAAAGGATTGGCTC
>seq_x5
TCTTGGTGGTAGTAGCAAATATTCAAATG
Here is the command I've tried using:
sed -n -r 's/^>(\d+)/>seq_x\1/' file
Using text editors on a small subset of the file, I have no problem with find and replace using '^>(\d+)' and '>seq_x\1', but I can't get sed to replace effectively. Is there something I'm missing?
In your simple case there's no need to specify a followed digit. Besides, you should use a more portable [0-9] (range of numbers) instead of \d+.
sed 's/^>/>seq_x/' file
This question already has answers here:
Non greedy (reluctant) regex matching in sed?
(27 answers)
Closed 6 years ago.
When I run a regex pattern from a online RegEx testing tool on the text below works fine. However, it is not working when using in sed on unix
Text:
<Field1><Field2><Field3>001</Field3></Field2><Field4><FieldDesc>Transaction Successful</FieldDesc></Field4><DtTm><LocalDtTm>2016-07-01-12:05:40.383</LocalDtTm></DtTm><Field5><Field6>N</Field6><Field7></Field7><DtTm><LocalDtTm>2016-07-01-12:05:44.171</LocalDtTm></DtTm></Field5></Field1>
RegEx:
<DtTm>(.*?)<\/DtTm>
Usage in Sed: Looking to remove anything between <DtTm> and </DtTm>
sed 's/<DtTm>(.*?)<\/DtTm>//g'
Expected Output:
<Field1><Field2><Field3>001</Field3></Field2><Field4><FieldDesc>Transaction Successful</FieldDesc></Field4><Field5><Field6>N</Field6><Field7></Field7></Field5></Field1>
GNU sed has two modes, basic and extended. Neither of these, nor the single basic mode of less advanced sed implementations, permit non-greedy specifications. As per the info sed output:
Note that the regular expression matcher is greedy, i.e., matches are attempted from left to right and, if two or more matches are possible starting at the same character, it selects the longest.
So, if you need non-greedy, you will have to choose another tool, such as Perl (or something else supporting PCRE), which is probably what the online testing tool you mentioned is using.
The good thing is, the Perl substitute command is so stunningly similar to the sed one that you can often just change the program name (and possibly use a different delimiter character in complex REs so you don't end up with sawtooths like \/\/\/\/\/):
perl -pe 's|<DtTm>.*?</DtTm>||g'