R-Markdown subscript in [square brackets] - r-markdown

This is my R-markdown code:
$\mu$Biomass~(Total)~[CO~2~#400ppm]
Everything works as it should, except for the subscript for the CO2. I'm guessing it's because it's in the square brackets, yes?
How should I go about getting subscript within square brackets?

Wrapping each square bracket with backticks solved my problem:
$\mu$Biomass~(Total)~`[`CO~2~ # 400ppm`]`
This now works perfectly!

Related

Regular Expression: brackets inside Square brackets

I would like to match the following string:
[-ed, -ing, -ment <i>n.</i>]
But exclude:
[-ed, -ing, -ment <b>n.</b>]
And my regex is:
\[[\-,\s\.(<i>)(</i>)a-z]+\]
Which won't work.
I add brackets to <i> so it appears as a whole, so <b> wont be matched.
However, brackets inside square brackets don't seem to work.
The following works with the sample string:
\[([^<>]|<i>.*?<\/i>)+?\]
I.e. square brackets containing a number of things that are either a single character that is neither < nor >, or <i>[...]</i> with some content.
It will match the first string and not the second. The problem description however is quite vague, so the regex might need some tweaking. E.g:
Is it just <i> or anything but <b>?
Can the square brackets contain nested square brackets?
Are the contents of the square brackets in fact comma-separated elements that must all begin with a hyphen?

How do I use a regex in perl like this (.+) for specified string?

I am using this code:
m/MSG\[(.+)\]/
to detect any character between square brackets and coloring it using this code:
if($currentLine=~m/MSG\[(.+)\]/){
$p1=$1;
$outp="$p1";
$currentLine=~s/MSG\[(.+)\]/MSG[$cg$outp$crs]/g;}
The above code colors to the end of the line and doesn`t detect the close square bracket. How can I get this to only color to the close square bracket?
for example, using this code, (.+) meaning:
((\?+\s+\?+)|(\?+\s+\d+)|(\w+\s+\?+)|(\w+\s+\w+)|(\d+)|(\w+)|(\?+)|(\d+\s+\w)|(\?+\d+)|(\?+\s)|(\d\?+))
are you understand the above code or explain for you?
How about:
$currentLine =~ s/MSG\[([^\]]+)\]/MSG[$cg$1$crs]/g;

Regex expression to extract everything inside brackets

I need to extract content inside brackets () from the following string in C++;
#82=IFCCLASSIFICATIONREFERENCE($,'E05.11.a','Rectangular',#28);
I tried following regex but it gives an output with brackets intact.
std::regex e2 ("\\((.*?)\\)");
if (std::regex_search(sLine,m,e2)){
}
Output should be:
$,'E05.11.a','Rectangular',#28
The result you are looking for should be in the first matching subexpression, i.e. comprised in the [[1].first, m[1].second) interval.
This is because your regex matches also the enclosing parentheses, but you specified a grouping subexpression, i.e. (.*?). Here is a starting point to some documentation
Use lookaheads: "(?<=\\()[^)]*?(?=\\))". Watch out, as this won't work for nested parentheses.
You can also use backreferences.
(?<=\().*(?=\))
Try this i only tested in one tester but it worked. It basically looks for any character after a ( and before a ) but not including them.

Visual Studio find and replace right square bracket ] in character class

I want to make a negated character class to match a square bracket tag like this [square bracket tag]. The problem is, the ] character ends the character class!
I tried
\[[^\]]+]
but I get a syntax error when I run it. (This is in the find and replace regex engine which is slightly different than the standard .NET engine fyi).
You forgot to escape the final end bracket:
\[[^\]]+\]
The first example in msdn uses \\ for escaping the \ which then escapes the .. So you should do something like \\[[^\\]]+\\] and also as Damien_The_Unbeliever said you haven't closed the final bracket.
I definitely expected escaping with "\" but it didn't work for me (grep#MacOS) but this:
[^]]
did the job.
Just place ] as the first character in class.
I actually used something like:
[^]?[]

Regex: How to get all contents inside a tag #[SOME TEXT HERE]

I am working on a simple token replacement feature of our product. I have almost resolved all the issue but I missed one thing. A token must support attributes, and an attribute can also be a token. This is part of a bigger project. hope you can help.
The begining tag is "**#[**" and the ending tag is "**]**". Say, #[FirstName], #[LastName], #[Age, WhenZero="Undisclosed"].
Right now i am using this expression "\#\[[^\]]+\]". I have this working but it failed on this input:
blah blah text here...
**#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]**
blah blah text here also...
It fails becauise it encouter the first ], it stops there. It returns:
*#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[Product_FreeShipping]*
My desired result should be
*#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]*
Your Regex matches exactly what your stated condition indicates : Start with an opening square bracket and match everything upto the first closing square bracket.
If you want to match nested square brackets, you need to specify exactly what is valid when nested. For instance, you could say that square brackets can be nested when enclosed within quotes.
This is a little border-line for a regexp, since it depends on a context, but still...
#\[(\](?=")|[^\]])+\]
should do it.
The idea is to mention a closing square bracket can be part of the parsed content if followed by a double quotes, as part of the end of an attribute.
If that same square bracket were anywhere within the attribute, that would be a lot harder...
The advantage with lookahead expression is that you can specify a regexp with a non-fixed match length.
So if the attribute closing square bracket is not followed by a double quote, but rather by another known expression, you just update the lookahead part:
#\[(\](?=</a>")|[^\]])+\]
will match only the second closing square bracket, since the first is followed by </a>".
Of course, any kind of greedy expression (.*]) would not work, since it would not match the second closing square bracket, but the last one. (Meaning if there are more the one intermediate ], it will be parsed.)
When I've done stuff like this before I've evaluated from the inner most matchable expression before stepping out to larger strings.
In this case your regex should probably try to replace $[FreeShipping] with it's value before evaluating the larger token containing the if clause.
Perhaps you can figure out a way to replace out the value token's like $[FreeShipping] before the ones without $ prepending the token
This is roughly but not exactly
http://en.wikipedia.org/wiki/Multi-pass_compiler versus http://en.wikipedia.org/wiki/One-pass_compiler
Writing this in one regex won't necessarily be any faster than looping over a few simple regex's. All regex's do is abstract string parsing.
If you're only expecting a single match in any given input you could simply allow for a greedy match:
/#\[.*\]/
If you're expecting multiples you have a problem because you no longer have regular text. You'll need to escape the inner brackets in some way.
(Regex is a deep subject - it's quite possible that someone has a better solution)
I'd be interested to lear if I'm wrong, but if I recall correctly, you cannot do this using regular expressions. This looks like a Dyck language to me and you would need a pushdown automaton to accept the expressions. But I must admit I'm not quite sure if this holds true for the extended form of regexp's like those provided by Perl.
It is possible to write a regex for the example you given but in general it fails. A single regex can't work for arbitrary nested expressions.
Your example shows that your DSL has 'if' conditions already. Not before long It could evolve into a Turing-complete language.
Why don't you use an existing template language such as Django template language:
Your example:
blah blah text here... #[IsFreeShipping,
WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]
blah blah text here also...
Using Django template language:
blah blah text here... {% if IsFreeShipping %}
<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>{{ FreeShipping }}</a>
{% endif %} blah blah text here also...
This works for your sample:
#\[(?:[^\]$]+|\$(?!\[)|\$\[[^\[\]]*\])*\]
It assumes that the inner square brackets can't themselves contain square brackets. If the inner tokens can also contain tokens, you're probably out of luck. Some regex flavors can handle recursive structures, but the resulting regexes are hideous even by regex standards. :D
Tis regex also treats the '$' as special only if it's followed by an opening square bracket. If you want to disallow its use otherwise, remove the second alternative: |\$(?!\[)