- Where Developers Learn, Share, & Build Careers


I am starting to learn regex and I do not know if I understand it correctly.

I have a problem with changing the function because it does not replace the character in a string that I want to change.

Here is my code:

  public class TestingRegex {public static void key (string algos []) {string string = "hello% l and +++ o_wow _ + L% D & # "; Four special family [] = {'%', '%', 'and', '_'}; (Four SC: special caterers) {if (string contains (sc + "")) string = string.replaceAll (sc + "", "\\" + sc); } System.out.println ("new string:" + string); }}   

Output is similar to original nothing changed.

I should have the output: Hell \% l & amp; +++ o \ _Wor \ _ ++ l \% d \ & amp; ## .

Please help in advance thanks.

The reason is not working: To create you need a four backslash in a Java string There is a single "real" backslash string = string.replaceAll (Sc, "\\\" + +);

This is not the right way to do this. You do not want to loop at all:

  string string = "hello% l & amp; +++ o_Wor _ ++ l% d & amp; # "; String = string.replaceAll (" [% & amp; _] "," \\\\ $ 0 ");   

And you have done < P> Explanation:

  • [% & amp; _] matches the three characters you want to change
  • $ 0 is the result of the match, so
  • "\\\\ $ 0" means "a backslash plus whatever It corresponds with Rijks. "

    WARNING: This solution is not obvious whether any of those characters already have already escaped or not.

      hollow \%   

    will be

      hollow \\%   

    you do that Do not want to have this problem?

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -