I am using Anypoint Studio 6.1 and Mule 3.8.1 and want to replace \n with new line and then remove \r, \t, \ from the payload text.
I can do this like:
#[payload.replace('\\n', System.getProperty('line.separator')).replace('\\r', "").replace('\\t', "").replace('\\', "")]
Is there a way to use OR or something similar for the checks on \r, \t, \ to reduce the code?
Thanks
You can make it a little bit shorter by using replaceAlland a regex like this:
#[payload.replace('\\n', System.getProperty('line.separator')).replaceAll("[\r|\t|']", "")]
Related
I am doing a mass method replace in my C# codebase. I have lines of code that look like the following:
Assert.That(Edit.FundsTable.GetCellByIndexes(0, 2).Text.Contains("Employer Request IPM A"));
The problem is that initially when the GetCellByIndexes call was made, we had another method that basically did the same thing, leaving us doing the exact same task 2 ways. The more standard way that we are changing it to is the following:
Assert.That(Edit.FundsTable.Cells[0, 2].Text.Contains("Employer Request IPM A"));
I am trying to do a VS replace all replacement to move GetCellByIndexes calls to Cells calls. The issue is with the right paran. I can do a replace all from
GetCellByIndexes(
to
Cells[
very easily. The problem is changing the right paran of the method call to a square bracket. Does anyone know how to identify the first right paran after the "GetCellByIndexes" string utilizing Regex?
Use
GetCellByIndexes\(([^()]+)\)
Replace with Cells[$1]. See proof.
Code
Explanation
GetCellByIndexes
'GetCellByIndexes'
\(
'('
(
group and capture to $1:
[^()]+
any character except: '(', ')' (1 or more times (matching the most amount possible))
)
end of $1
\)
')'
In general search for:
GetCellByIndexes\(\s*(\d+)\s*,\s*(\d+)\s*\)
replace with
Cells[$1, $2]
Both if you are using the search-and-replace of Visual Studio or if you are programming in C#.
Note that this will only work if the indexes are numbers... If they are something more complex (variables, or functions) then it becomes more interesting (and complex).
I am trying to replace ~ with a newline \n using replace(p){r, t} conversion in logback.xml.
I have the pattern layout like
%p %c [%t] \\(%M:%L\\) - %replace(%msg){'~', '\n'} %nopex %n
When I put \n, logback changes it to 'n'. But if I put '\\\n' then it keeps both the backslashes i.e. it retains '\\\n'. Before getting deep into the source code of logback, I wanted to check if anybody ever tried/faced it?
To me, it seems we can't add newline to the message using the replace method!
It adds an extra escape character (\\).
A possible workaround is to use regex capture group:
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %replace(%msg){'(\n)','$1'}%n</pattern>
But it is still not possible to add other escaped char like a tab '\t'
I opened an issue https://jira.qos.ch/browse/LOGBACK-1261
To add escaped characters in logback xml configuration file, use xml character entity reference. In your case:
%p %c [%t] \\(%M:%L\\) - %replace(%msg){'~', '
'} %nopex %n
Similarly, to replace \n in exception stack traces to \r, one could do (say if you are scraping the logs through an operator & ingesting it in to elastic):
%replace(%ex){'\n', '
'}
Source: Noisyfox's comment at https://github.com/symphoniacloud/lambda-monitoring/issues/4
I suck at regular expressions and what to do a simple find and replace of '}' to '} /n' notepad++ can recongise what I'm after as it has the 3 options of normal find and replace, special chars and full regex. I however only ever used to option two. How can I enable special characters in my search using sublime text 2.
Cheers
Joe,
In sublime you can use find & replace. Drag the replace panel large enough and use cmd + enter to go to a new line.
Hit replace all:
And thats it your sorted chap
If you want to replace } within notepad++ with } \n you can use the following rules:
find: }
Replace with: } \n
Searchmethod: Extended (\n, \e...)
Press replace all
I need to replace all instances of / character with \ between < filename >...< / filename > tags.
The file has like 2.000 of those tags and I only need to replace the / character inside those tags.
How can i do?
Edit: Given the new information, the below substitution would probably work:
:%s/<filename>\zs.\{-}\ze<\/filename>/\=substitute(submatch(0), '\/', '\', 'g')/
Explaination:
%s: substitute across the entire file
/<filename>: start of pattern and static text to match against
\zs: start of the matched text
.\{-}: any character, non greedy
\ze: end of matched text
<\/filename>/: end of targeted tag and pattern
\=: evaluate the replacement as a vim expression
substitute(submatch(0), '\/', '\', 'g')/: replace all /'s with \ in the matched text.
Original answer:
I'm going to assume you mean XML-style tags here. What I would do is visually select the area you'd like to operate on, then use the \%V regex atom to only operate on that selection.
vit:s!\%V/!\\!g
Should do the trick. Note that when pressing :, vim will automatically add a range for the visual selection, the actual substitution command will look like:
:'<,'>s!\%V/!\\!g
Iff we can assume that the tags are on single lines, it is simply:
Note Enter ^M as C-vC-m (C-qC-m on windows)
:g/<filename>/norm! /filename>/e^Mvity:let #"=substitute(#", '/', '\\', "g")^Mgvp
Hmmm integrating the hint by Randy on using \%V in a pattern makes it simpler:
:g/<filename>/norm! /filename>/e^Mvit:s#\%V/#\\#g^M
I tested both. Whoo. I'll explain now. Hold on.
:g/<filename>/ - _for each line containing <filename>
norm! - _execute normal commands (ignoring mappings)
/filename/eEnter jump to the end of the open tag
vit - select the inner text of that tag in visual mode
:s#\%V/#\\#gEnter - _on that visual selection, perform the substitution (replace \ by /)
VIM has a sharp learning curve, as do regex's. I believe this command will do it. You have to escape each char with '\'.
:%s/\//\\/g
G'day,
I am using the following Perl fragment to extract output from a Solaris cluster command.
open(CL,"$clrg status |");
my #clrg= grep /^[[:lower:][:space:]]+/,<CL>;
close(CL);
I get the following when I print the content of the elements of the array #clrg BTW "=>" and "<=" line delimiters are inserted by my print statement:
=><=
=>nas-rg mcs0.cwwtf.bbc.co.uk No Online<=
=> mcs1.cwwtf.bbc.co.uk No Offline<=
=><=
=>apache-rg mcs0.cwwtf.bbc.co.uk No Online<=
=> mcs1.cwwtf.bbc.co.uk No Offline<=
=><=
When I replace it with the following Perl fragment the blank lines are not matched.
open(CL,"$clrg status |");
my #clrg= grep /^[[:lower:][:space:]]{3,}/,<CL>;
close(CL);
And I get the following:
=>nas-rg mcs0.cwwtf.bbc.co.uk No Online<=
=> mcs1.cwwtf.bbc.co.uk No Offline<=
=>apache-rg mcs0.cwwtf.bbc.co.uk No Online<=
=> mcs1.cwwtf.bbc.co.uk No Offline<=
Simple question is why?
BTW Using {1,} in the second Perl fragment also matches blank lines!
Any suggestions gratefully received!
cheers,
That'll be because [:space:] matches newlines and carriage returns as well.
So [[:space:]]+ would match \n, \r\n, or \n\n.
But [[:space:]]{3,} would require three characters, and an empty line is just a \n.
{1,} and + mean the same thing: match the preceding group one or more times.
P.S. A typical newline is \n on Unix and \r\n on Windows.
Hm. According to the Perl regular expression documentation, the [:space:] character class should not include newlines, as it is supposed be the equivalent of \s (except that it recognizes an additional character, vertical-tab, to maintain POSIX compliance).
However, having just tested this on 5.10.0, I can verify that it is matching newlines as well. Whether this qualifies as a bug in Perl or in the documentation, I'll leave for the Perl maintainers. But to avoid the immediate problem, use the previous answerer's solution and just use \s instead of the POSIX class.