Is there a Powershell regex command I could use to replace the last consecutive zero in a text string with a "M". For Example:
$Pattern = #("000123456", "012345678", "000000001", "000120000")
Final result:
00M123456
M12345678
0000000M1
00M120000
Thanks.
Search for the following regex:
"^(0*)0"
The regex searches for a consecutive string of 0 at the beginning ^ of the string. It captures all the 0 except the one for replacement. "^0(0*)" also works, since we only need to take note of the number of 0 which we don't touch.
With the replacement string:
'$1M'
Note that $1 is denotes the text captured by the first capturing group, which is (0*) in the regex.
Example by #SegFault:
"000120000" -replace "^(0*)0", '$1M'
Related
I'm trying to do a regex expression in powershell to get only a specific part of a string. I know a way I can do this without regex but it can definitely be more efficient with. I have a string that looks like this:
Some/Stuff/Here/Then.drop.last
Ideally, I want to write a regex that gets me just:
Then.Drop
PS> 'Some/Stuff/Here/Then.drop.last' -replace '.*/(.+)\..*', '$1'
Then.drop
.*/ greedily matches everything up to the last /
(.+)\. greedily matches everything up to the last literal . and captures everything before that . in the first capture group ($1) - which is your string of interest.
.* matches the remaining part of the string.
Using $1 as the replacement string then replaces the overall match - the entire input string - with what the first capture group matched.
For more information about PowerShell's -replace operator, see this answer.
I am trying to get the value inside specific quotes from Wordpress content string. I managed to get content but I couldn't use the correct preg_match function.
Example content is:
[vc_single_image image="1667" img_size="full" alignment="center" onclick="img_link_large" css_animation="fadeIn"]
From this string, I am trying to get the value "1667" by using preg_match function.
I have this code right now:
$regex = '/"([^"]+)"/';
$output = preg_match($regex, $post->post_content, $matches);
$first_img_id = trim($matches[0][21], '"');
When I echo out this code, it is working properly. But I am getting the value by checking only quotes. If I add more strings with quotes, then my code will not work properly. Therefore, I want to get the image id with "image=" part. How should I change my regex and matches array indexes?
Your string is a set of key-value pairs inside square brackets. You may match any specific key value by using a preg_match function with a regex like
"~[\s[]$key=\"\K[^\"]*~"
See the regex demo for the current scenario with image.
The [\s[] matches either a whitespace, or [ char, that is, it matches the left boundary of the key. $key is a variable that should not contain non-word chars, and in real life, keys usually consist of word chars. =" matches an equal sign and a quote after it, but the \K match reset operator clears the match value, and only the text that is matched by [^"]* (zero or more chars other than ") gets put into the match memory buffer, and that is what you get by accessing the first item in the resulting $matches array.
See the online PHP demo:
$key="image";
$regex = "~[\s[]$key=\"\K[^\"]*~";
$content='[vc_single_image image="1667" img_size="full" alignment="center" onclick="img_link_large" css_animation="fadeIn"]';
if (preg_match($regex, $content, $matches)) {
echo $matches[0];
}
Output: 1667
For the records, you could get all key/value pairs immediately with
(?:\G(?!\A)|\[)
[^][]*?\K
(?P<key>\w+)="(?P<value>\w+)"
See a demo on regex101.com.
It might be an option to first use trim to remove the [ and ] from the beginning and end from the string and use explode using a whitespace as the delimiter.
Then you could loop the array from explode and use a regex like for example ^image="(\d+)"$ to match image="1667"in the loop.
If it matches, capturing group 1 (\d+) will contain your value
^image="(\d+)"$ will match:
^ Assert position at the start of the string
image=" Match literally
(\d+) Match one or more digits in a capturing group
" Match literally
$ Assert position at the end of the string
Demo php
Using REGEX (in PowerShell) I would like to find a pattern in a text file that is over two lines and replace it with new text and preserve the whitespace. Example text:
ObjectType=Page
ObjectID=70000
My match string is
RunObjectType=Page;\s+RunObjectID=70000
The result I want is
ObjectType=Page
ObjectID=88888
The problem is my replacement string
RunObjectType=Page;`n+RunObjectID=88888
returns
ObjectType=Page
ObjectID=88888
And I need it to keep the original spacing. To complicate matters the amount of spacing may change.
Suggestions?
Leverage a capturing group and a backreference to that group in the replacement pattern:
$s -replace 'RunObjectType=Page;(\s+)RunObjectID=70000', 'RunObjectType=Page;$1RunObjectID=88888'
See the regex demo
With the (\s+), you capture all the whitespaces into the Group 1 buffer and then, using $1 backreference, the value is inserted into the result.
I am using the following expression:
Find what: [0-9]
But what should I write in Replace with field if I want to add specific sup tag to all the digits?
Thanks in advance!
The replacement can be
<sup>$0</sup>
or
<sup>$&</sup>
Note that the $0 / ${0} / $&, or even $MATCH and ${^MATCH} backrefrence inserts the whole match.
See the Substitutions section:
$&, $MATCH, ${^MATCH}
The whole matched text.
and
$n, ${n}, \n
Returns what matched the subexpression numbered n. Negative indices are not alowed.
Note that a match value is usually stored as Group 0 inside a match object.
However, \0 as of now does not work (Notepad++ v.6.9), it looks like it is treated as a NUL character and truncates the replacement pattern right at the location where it is located.
Let's say I want to add a 0 after every word
(\w+)
The following replacement string doesn't work.
$10
So, how do I convert
this is my string
into
this0 is0 my0 string0
Use braces around the group ID in the replacement string:
${1}0
The braces tell the regex engine that the number inside them is the actual Group ID. The 0 that will follow will be treated as a literal zero.
BTW, you can also get the same result with \w+ regex and ${0}0 replacement string, no need in capturing groups.
Or, using \n syntax, it works like this:
Find: (\w+)
Replace: \10