Correct. escape sequence for over bar? - java

I am using a string as my source for an equation, and whenever I try to add something like an overbar tag which is:
\ov5\ - creates a bar over the 5
However, when I add this into a Java string, for it to compile I am required to write it like this:
string x= "\\ov5\\";
It would appear that this way breaks JQMath and doesn't work, resulting in a broken equation. Here is the code in case I did something terribly wrong:
WebView webView;
String functext = "$$\\ov55\\$$";
js = "<html><head>"
+ "<link rel='stylesheet' href='file:///android_asset/mathscribe/jqmath-0.4.3.css'>"
+ "<script src='file:///android_asset/mathscribe/jquery-1.4.3.min.js'></script>"
+ "<script src='file:///android_asset/mathscribe/jqmath-etc-0.4.3.min.js'></script>"
+ "</head><body>"
+ functext + "</body></html>";
webView.loadDataWithBaseURL("", js, "text/html", "UTF-8", "");
EDIT: For clarification, the end result oddly reads "$$\ov55$$".
Please note that when I try the same string on JQMath's website page here, it works as intended.
EDIT2: Here are some debug values for a breakpoint placed at webView.loadDataWithBaseURL:
actual string: String functext = "$$\\\\ov55\\\\$$";
actual displayed result: $$\ov55\$$
debug results:
functext = $$\\ov55\\$$
js = <html><head><link rel='stylesheet' href='file:///android_asset/mathscribe/jqmath-0.4.3.css'><script src='file:///android_asset/mathscribe/jquery-1.4.3.min.js'></script><script src='file:///android_asset/mathscribe/jqmath-etc-0.4.3.min.js'></script></head><body>$$\\ov55\\$$</body></html>
Any help with loading it in another way other than a string would help greatly.

I think you want this:
String functext = "$$\\ov55\\$$";
(The first \ needs to be before the ov operator.)
EDIT: Another possibility (since the above was evidently just a typo in your post, not in your code) is that somewhere in the pipeline the string is being interpolated a second time. In that case, you would need to double-escape the backslashes:
String functext = "$$\\\\ov55\\\\$$";
P.S. If the end result reads "$$\ov55$$" then the problem seems to be before jqmath sees anything. The code you posted definitely does not produce that result for me.

Also jqMath accepts ` (backquote) in place of \ if that makes things easier. Finally, I'd put a space between the ov and the 5 to clarify that it's not a macro named ov5. (Plus see my comment above to remove the final \.)

Related

Printing an attribute inside quotes

I want to print an attribute of a class I am making , but I need it to be printed inside quotes " ".
I know it has something to with Escape Sequences but a similar post I found suggested using "\"Hello\"" for example to print "Hello"... My case is a bit more complicated cause I don't know beforehand the value of the attribute I want to print. So how can I do this?
Why don't make a function that make any String into a Quote. Exanple
public static String quotePrinter(String myQuote)
{
return "\"" +myQuote+ "\"";
}
String myQuote = "Hello World";
System.out.println(quotePrinter(myQuote));
And the output
"Hello World"
Not sure if i understand corretly want you want but take a look at the answers from #ataylor and #Martin Törnwall How to format strings in Java for String interpolation

Replace String with characters with substring

I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps

Need java Regex to remove/replace the XML elements from specific string

I have a problem in getting the correct Regular expression.I have below xml as string
<user_input>
<UserInput Question="test Q?" Answer=<value>0</value><sam#testmail.com>"
</user_input>
Now I need to remove the xml character from Answer attribute only.
So I need the below:-
<user_input>
<UserInput Question="test Q?" Answer=value0value sam#testmail.com"
</user_input>
I have tried the below regex but did not worked out:-
str1.replaceAll("Answer=.*?<([^<]*)>", "$1");
its removing all the text before..
Can anyone help please?
You need to put ? within the first group to make it none greedy, also you dont need Answer=.*?:
str1.replaceAll("<([^<]*?)>", "$1")
DEMO
httpRequest.send("msg="+data+"&TC="+TC); try like this
Although variable width look-behinds are not supported in Java, you can work around it with .{0,1000} that should suffice.
Please check out this approach using 2 regexes, or 1 regex and 1 replace. Choose the one that suits best (I removed the \n line break from the first input string to show the flaw with using simple replace):
String input = "<user_input><UserInput Question=\"test Q?\" Answer=<value>0</value><sam#testmail.com>\"\n</user_input>";
String st = input.replace("><", " ").replaceAll("(?<=Answer=.{0,1000})[<>/]+(?=[^\"]*\")", "");
String st1 = input.replaceAll("(?<=Answer=.{0,1000})><(?=[^\"]*\")", " ").replaceAll("(?<=Answer=.{0,1000})[<>/]+(?=[^\"]*\")", "");
System.out.println(st + "\n" + st1);
Output of a sample program:
<user_input UserInput Question="test Q?" Answer=value0value sam#testmail.com"
</user_input>
<user_input><UserInput Question="test Q?" Answer=value0value sam#testmail.com"
</user_input>
First off, in your sample above, there is a trailing " after the email and > which I do not know if it was placed by error.
However, I will keep it there as according to your expected result, you need it to still be present.
This is my hack.
(Answer=)(<)(value)(>)(.+?([^<]*))(</)(value)(><)(.+?([^>]*))(>) to replace it with
$1$3$5$8 $10
The explanation...
(Answer=)(<)(value)(>) matches from Answer to the start of the value 0
(.+?([^<]*) matches the result from 0 or more right to the beginning < which starts the closing value tag
(</) here, I still select this since it was dropped in the previous expression
(><) I will later replace this with a space
(.+?([^>]*) This matches from the start of the email and excludes the > after the .com
(>) this one selects the last > which I will later drop when replacing.
The trailing " is not selected as I will rather not touch it as requested.

replaceAll replacing the full words before and after slash as well

I have a need wherein I need to replace some specific words.
For example, if my text has
He needs to have java skills
I need to replace it as
He/She needs to have java skills
I kind of achieved this with below code
String replacedText = originalText.replaceAll("\\bHe\\b|\\bShe\\b","He/She");
But the problem is when I execute the code again, the output is
He/She/He/She needs to have java skills
The problem is '\\b' is considering the words full even when they are before or after slash.
Update: I am getting the source from a word/excel/html file. So for the first time it works fine. My intention is if I run the code again on the modified files, it should not change anything.
How to fix this?
Few hints at start:
he she can re represented with s?he (where s is optional) so you don't need he|she (it will keep things shorter and equally simple).
Also you can use (?i) flag which will make your regex case-insensitive.
Now consider replacing either
he
she
but also
he/she
she/he
with he/she. Regex representing this cases can look like s?he(/s?he)?
So try with
replaceAll("(?i)\\bs?he(/s?he)?\\b","He/She");
I achieved it with the help of negative lookahead and negative lookbehind. With this logic I can run the code any no. of times on already modified files as well.
private String replace(String originalText) {
String replacedText = originalText.replaceAll(
"\\b(he(?!/)|(?<!/)she)\\b", "he/she");
replacedText = replacedText.replaceAll("\\b(He(?!/)|(?<!/)She)\\b",
"He/She");
replacedText = replacedText.replaceAll("\\b(his(?!/)|(?<!/)her)\\b",
"his/her");
replacedText = replacedText.replaceAll("\\b(His(?!/)|(?<!/)Her)\\b",
"His/Her");
replacedText = replacedText.replaceAll("\\bhim(?!/)\\b", "him/her");
replacedText = replacedText.replaceAll("\\bHim(?!/)\\b", "Him/Her");
return replacedText;
}
Thank you Biffen for the idea.
A simple approach could be
String[] originalTexts = {"He needs to have java skills",
"She needs to have java skills",
"He/She needs to have java skills"
};
for (String original : originalTexts) {
String replacedText = original.replaceAll("\\b(She/He|He/She|He|She)\\b","He/She");
System.out.printf("original: %-32s replacedText: %20s%n", original, replacedText);
}

Regular expressions in java

String s= "(See <a href=\"/wiki/Grass_fed_beef\" title=\"Grass fed beef\" " +
"class=\"mw-redirect\">grass fed beef.) They have been used for " +
"<a href=\"/wiki/Paper\" title=\"Paper\">paper-making since " +
"2400 BC or before.";
In the string above I have inter-mixed html with text.
Well the requirement is that the output looks like:-
They have been used for paper-making since 2400 BC or before.
Could some one help me with a generic regular expression that would produce the desired output from the given input?
Thanks in advance!
The following expression:
\([^)]*?\)|<[a-zA-Z/][^>]*?>
will match anything that looks like an HTML tag and any parenthesized text. Replace said text with "", and there ya go.
Note: If you try to match any string that has script tags in it, or "HTML" where the author didn't bother to escape < and > when they weren't used as tag delimiters), or a ( without a ), things will probably not work as you'd hoped.
https://stackoverflow.com/questions/1732348#1732454
You have been warned.

Categories

Resources