Transform an expression to another with regex for a replace - regex

I need to replace a lot of strings in an application and I can do it with regex but I don't know how.
My current string is: {$str.LOREM} or {$str.LOREM_IPSUM}.
And the output desired is: <?php echo i18n::n('example.lorem');?> or <?php echo i18n::n('example.lorem_ipsum');?>.
UPDATE: Due to confussion in a answer: I want to do it with my IDE. I have like 500 different strings. Netbeans let me use regular expressions and I would like to find one that works with the above example. If possible if is not need to write all the 500 to change, it'll be beter. Thanks!
How I can do it?

If your string looks like that, you dont need regex, as that will be pretty slow. You can use the faster str_replace method for that.
Its as simple as:
$content = str_replace('{$str.LOREM}', i18n::n('example.lorem'), $content);
$content = str_replace('{$str.LOREM_IPSUM}',i18n::n('example.lorem_ipsum'),$content);

Related

How to use Regex to replace a tag in a word document with Powershell

First post on stackoverflow for me so sorry if something is out of norm or similar ^^
Currently I'm trying to find a way to read vouchers out of a .csv that I get from my pfsense.
Plan is to read it out of the .csv and write it down in a Word document so that secretaries can print it out and give them out to coworkers.
So far I have no problems replacing names and room numbers, all I gotta do now is to find a way to replace the voucher codes, but since they obviously always change I tried to use regex, here's the current state of that part of my code:
if ($Vouchers -match '((\d|\w){11})*') {
$matches.0 }
ReplaceTag –Document $Doc -FindText ‘<Vouchers>’ -replacewithtext $matches
The regex itself is working perfectly fine (already tested it on regex101) so I guess it's the code.
I'm assuming that it's trying to literally match "((\d|\w){11})*" instead of using the pattern :\
Any kinda help would be welcomed!

Matching a substring using a regular expression in PowerShell

Regular expressions are really not my forte and I am trying to learn. Struggling with this one at the moment.
<fraglink id="230681395" resid="1057000484">
I have a file with loads of text in it, and every now and then bits like the above appear in it. I want to get the number in between the quotes after resid=.
Is some sort of look ahead / behind required here?
It looks like you want a regex like:
resid="([0-9]+)"
And to grab $1.
Since your content looks like XML, you should probably not use a regular expression to grab your desired value. If you share your whole file we will show you how to select the value properly using XPath for example.
However, if you want to use a regular expression for training purposes, try this:
$content = Get-Content 'your_file_path' -raw
[regex]::Match($content, '\bresid="([^"]+)').Groups[1].Value
Using a lookahead ((?<=pattern)), your pattern could look like:
(?<=resid=")\d+
With Regex.Match():
$id = [regex]::Match($inputString,'(?<=resid=")\d+').Value

Textmate Regex Issue

I have a very large .CSV document with text I need removing. The data looks like this
774431994&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774431994
774431996&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774431996
774431998&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774431998
774432000&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774432000
774432003&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774432003
774432006&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774432006
774432009&images=774431994,774431996,774431998,774432000,774432003,774432006,774432009&formats=0,0,0,0,0 /1/6/9/5/2/6/8/webimg/774432009
I'm using the following Regex which is working on http://regexr.com/3a6oa
/.{128}(?=webimg).{10}/g
It just doesn't seem to work with Textmate Search. Does anyone know why? I need to select all of this junk and replace it with nothing, the numbers are unique each time.
Thanks very much
Why are you using a lookahead in your pattern? Just use: /.{128}webimg.{10}/g
Why are you using Textmate search at all? I'd need to know more context of the problem to say for sure, but I bet a simple sed command could just be used instead:
sed -i "webimg/d" ./filename.csv

I need some help doing a regular expression

I'm using netbeans and I want to wrap $this->escape(stuff) around specific stuff in more files, using the replace feature, but there's plenty of conditions, like:
match:
$this->$variable, $variable
$this->$array['something'], $this->$array[0], $array['something'], $array[0]
do not match:
$this->partial, $this->escape, $this->form
What I have so far that works ok
replace this
echo\s+(\$this->[->a-zA-Z_']+[^\s;(])
with this
echo \$this->escape\($1\)
It doesn't work with arrays and doesn't exclude anything, also doesn't match $variable
Could anyone help out, improve the expression so that it would reduce the torture of going through so many strings by hand? Any help is appreciated.
edit: just adding the exclusions to my working example would be enough.
It's not entirely clear what parts you're trying to match. If I understood you correctly then this should work:
(?:\$this->)?\$[^,\s-]*(?=[,\s])
If you meant to match something else, please let me know.

regular expression to replace '<?' by '<?php'

Hi i'm trying to find all occurences of '<?' in my php code. I use eclipse for this, and i tried to search for the folowing pattern: "<\?^[p]" in the hope this would return me all <? occrences, but not the <?php ones..
What's wrong about the regex? I thought I had figured out regular expressions, but it seams like i still have a long way to go :(
try this: <\?[^p]
I'm not familiar with Eclipse's regular expression language but I'd imagine you want this:
"<\?[^p]"
The difference is:
[^p] means any character apart from p
^[p] means the start of a new line followed by a p.
But you should check the manual for Eclipse to find out the exact regular expression syntax that Eclipse uses.
If all fails, you could use a trick with no regexes: replace your <?php occurrences with something else (like for example THE_PHP_TAG), then search for <?, then replace THE_PHP_TAG back to <?php.
Edited
From what I understand you want to change <? to <?php. Try replacing <\?(?!php) with <?php. (This will prevent <?php from turning into <?phpphp).