I know this is a very specific question by I didn't manage to do the replacement myself, I need to replace this string in groovy:
com.pantest in com/pantest.
I tried this:
groupId =com.pantest
def mavenGroupID = groupId.replaceAll('.','/')
And this is what I get in the output:
echo mavenGroupID is //////////
mavenGroupID is //////////
Is the dot (.) is some kind of special character? I tried to escape it using **** but it also didn't work.
As mentioned in the comments, String.replaceAll is taking regex as the input, so it means you need to escape dot at least, but actually, you have also to escape escaping char- backslash \
(more clues at Regular Expression to match a dot)
so, you can do it like follows:
def test = "aaa.bbb.ccc"
//want to replace ., need to use escape char \, but it needs to be escaped as well , so \\
println test.replaceAll('\\.','/')
Output is as requested aaa/bbb/ccc
replaceAll('\\.','/') is the key
Related
Hi i am trying to replace a string with special character at the end with new string. For Example, I want to replace
qwerty_CRS_abc\
to
qwerty_CRS_abc
I tried with this:
:%s/qwerty_CRS_abc\/qwerty_CRS_abc/g
but I'm getting this error:
Pattern not found: padring_CRS_CAN\/padring_CRS_CAN\g
Basically, I just want to remove that backslash in whole file. It should be just
qwerty_CRS_abc
Use:
:%s/qwerty_CRS_abc\\/qwerty_CRS_abc/g
Certain characters such as /&!.^*$\? carry a special significance to the search process and must be escaped using the \ character when they are used in a search. Hence the \\ used to escape the backslash in your example.
how I could use eval(rlang::parse_expr(string))’ or alternative with such expresssion string <-"print('A\s*B')"`? I am getting unrecognized escape character. The expression is evaluated inside function, print is an example, I am using grepl in similar manier.
Simply using second escape slash helped "print('A\s*B')"
I'm struggling to get an ES6 template literal to produce a single backslash it its result.
> `\s`
's'
> `\\s`
'\\s'
> `\\\s`
'\\s'
> `\\\\s`
'\\\\s'
> `\u005Cs`
'\\s'
Tested with Node 8.9.1 and 10.0.0 by inspecting the value at a Node REPL (rather than printing it using console.log)
If I get your question right, how about \\?
I tried using $ node -i and run
console.log(`\\`);
Which successfully output a backslash. Keep in mind that the output might be escaped as well, so the only way to know you are successfully getting a backslash is getting the character code:
const myBackslash = `\\`;
console.log(myBackslash.charCodeAt(0)); // 92
And to make sure you are not actually getting \\ (i.e. a double-backslash), check the length:
console.log(myBackslash.length); // 1
It is a known issue that unknown string escape sequences lose their escaping backslash in JavaScript normal and template string literals:
When a character in a string literal or regular expression literal is
preceded by a backslash, it is interpreted as part of an escape
sequence. For example, the escape sequence \n in a string literal
corresponds to a single newline character, and not the \ and n
characters. However, not all characters change meaning when used in an
escape sequence. In this case, the backslash just makes the character
appear to mean something else, and the backslash actually has no
effect. For example, the escape sequence \k in a string literal just
means k. Such superfluous escape sequences are usually benign, and do
not change the behavior of the program.
In regular string literals, one needs to double the backslash in order to introduce a literal backslash char:
console.log("\s \\s"); // => s \s
console.log('\s \\s'); // => s \s
console.log(`\s \\s`); // => s \s
There is a better idea: use String.raw:
The static String.raw() method is a tag function of template
literals. This is similar to the r prefix in Python, or the #
prefix in C# for string literals. (But it is not identical; see
explanations in this issue.) It's used to get the raw string form
of template strings, that is, substitutions (e.g. ${foo}) are
processed, but escapes (e.g. \n) are not.
So, you may simply use String.raw`\s` to define a \s text:
console.log(String.raw`s \s \\s`); // => s \s \\s
I want to replace a as using C#. I could not able to achive this using Regex.Replace functions as follos
Regex.Replace(html, "\\"", "\"");
execution this command again produces the original output
Anyone have already faced issue like this,Any help would be of greatly appreciated.
Regards,
Ganesan
first of all "\\""produces a compiler error, since you are just escaping one backslash but not the quote.
you are working with 2 escape mechanisms here, one is from the c# compiler, and another is from the regex interpreter.
Which means:
when you give this C# string as a regex: "\\\"" then after compilation there is a string looking like that \", which is then interpreted by the regex engine, which also uses \ as the escape character. therefor regex will escape ", so your code will replace " with "
so if you now use "\\\\\"", first the c# compiler will make \\" out of that, then the regex engine will make \" out of that (both are using \ as escape character)
now c# has a nice little feature to make such strings easier to write.
if you add an # before your string, \ will no long be the escape character, but now you have to escape " with ""
that means "\\\"" == #"\""" and "\\\\\"" == #"\\"""
so you could write Regex.Replace(html,#"\\""","\"")
which is easier to read then Regex.Replace(html,"\\\\\","\"")
i think i got it right this time :D
I have several php files in directory, I want to replace a few words in all files with different text. It's a part of my code:
$replacements_table=
("hr_table", "tbl_table"),
('$users', "tbl_users")
foreach ($file in $phpFiles){
foreach($replacement in $replacements_table){
(Get-Content $file) | Foreach-Object{$_ -replace $replacement} | Set-Content $file
}
}
It works fine for replacing "hr_table", but doesn't work at all for '$users'. Any suggestion would be nice
The string is actually a regular expression and so needs to be escaped using '\'. See this thread
$replacements_table= ("hr_table", "tbl_table"), ('\$users', "tbl_users")
will work.
The dollar sign is a special regular expression character, matches the end of a string, you need to escape it. Escaping a character in regex is done by a '\' in front of the character you want to escape. A safer method to escape characters (especially when you don't know if the string might contain special characters) is to use the Escape method.
$replacements_table= (hr_table', 'tbl_table'), ([regex]::Escape('$users'), 'tbl_users')
Try escaping "$' with a backslash: '\$users'
The $ symbol tells the regular expression to match at the end of the string. The backslash is the regular expression escape character.
try using double quotes around your variable name instead of single quotes
EDIT
Try something along these lines ....
$x = $x.Replace($originalText, '$user')