I am wondering what the correct syntax for multiple expressions within th:if in Thymeleaf is.
So far I have something like this:
<a style="color:blue" th:href="#{/approveAndJoin/{name}(name=${outbox.receiverName})}" th:if="${not #strings.startsWith(outbox.status, 'P')}">Join Team?!</a>
What would the correct syntax be to do something like this:
(Pseudo code)
th:if="${not #strings.startsWith(outbox.status, 'P') && #strings.startsWith(inbox.status, 'A'}"
I have read through the various manuals but have not discovered how to do this.
You can combine several conditions with the keyword and
e.g. in your case th:if="${not #strings.startsWith(outbox.status, 'P') and #strings.startsWith(inbox.status, 'A'}"
Related
I want to evaluate multiple conditions in one If statement in Power Automate Desktop. I have been able to do it with the basic operators (=, >, <, <>, etc.) and using IsEmpty. But I have not found any documentation about how to use it with these:
Contains
Does not contain
Starts with
Doesn't start with
Ends with
Doesn't end with
I would like to evaluate something like this:
%strFileContents Contains 'word' and strFilePath Does not contain '.txt'%
I know that I can achieve this with multiple ifs but I would like to know if it can be done in a single one.
Thanks.
%Contains(strFileContents, 'Word', True) AND NOT(Contains(strFilePath,'.txt.))%
You can add as many AND or OR as you like to evaluate the condition.
In one of my script I use urllib2and BeautifulSoup to parse a HTML page and read a <script> tag.
This is what I get :
<script>
var x_data = {
logged: logged,
lengthcarrousel: 2,
products : [
{
"serial" : "106541823"
...
</script>
My goal is to read the JSON in the x_data variable and I do not know how to do it properly.
I though of :
Convert to string and remove the first chars to the { and same for last }
Use Regular Expression with something like '{.*}' and take the first group
Something else ?
I don't know if these are efficient and if there is some other ways to do it in a nice way.
Do you think a method is preferable to the other ? any method I may not be aware of ?
Thank you in advance for any advice.
EDIT :
Following advice I get the Regexp solution but I can't search in multiple lines despite using re.MULTILINE :
string1 = '<script>
var x_data = {
logged: logged,
lengthcarrousel: 2,
products : [
{
"serial" : "106541823"}
]
};
</script>'
p = re.compile(r'\{.*\};',re.MULTILINE);
m = p.search(string1)
if m:
print m.group(0)
else:
print "Error !"
I always got an "Error !".
EDIT2 :
Works well with re.DOTALL.
I think these methods are essentially the same in terms of elegance and performance (using {.*} may be slightly better because .* is greedy, i.e. there will be almost no backtracking, and because it seems to me more "forgiving" for different JS code formatting nuances). What you may be more interested in is this: https://docs.python.org/3.6/library/json.html.
If it always looks exactly like this, then you can hack a solution like the one you proposed, based on it looking exactly like this.
Because programmers do everything in code, I suspect in practice it will not alway look exactly this, and then any hacky solution will be fragile and will fail at unexpected (read "impossibly inconvenient") moments. (Regex is known to be hacky when it comes to parsing code).
If you want to do this right, you will need to get a real JavaScript parser, apply it to the code fragment defined by the script tag content, to produce an AST, then search the AST for JavaScript nested structures that happen to look like JSON, and take the content of that tree, prettyprinted.
Even this will be fragile in the face of a programmer who assembles the JSON fragment using JavaScript assignment statements. You can handle this by computing data flow, and discovering sets of code that happen to assemble JSON code. This is rather a lot of work.
So you get to decide what the limits on your solution will be, and then accept the consequences when somebody you don't control does something random.
I'm using regular expression in AutoWikiBrowser to replace the input of several values with just one value, such as this:
|value1=4
|value2=5
|value3=6
To this:
|value={{#expr:4+5+6}}
While the correct result does show on the page, it does not look good in the code itself, so I'm trying to find a way to make it the result only (in this case value=15) but so far no luck. Can someone help me with out showing how to make this possible?
P.S. I tried the search function but didn't find a similar question.
MediaWiki parser allows the templates can be subst'ed, ie replaced by their rendering. That's also true for parser functions call.
You can subst a template prefixing the template call by subst:.
|value={{subst:#expr:4+5+6}}
Reference: Substitution on MediaWiki manual
Example: diff (the expression used is in the edit summary, the result in the diff)
Can you please help me with a regular expression that I need for some code refactoring? I am trying to change the following
something a.executeQuery() something else
something b.executeQuery() something else
something c.executeQuery() something else
to
something someClass.executeQueryEx(a) something else
something someClass.executeQueryEx(b) something else
something someClass.executeQueryEx(c) something else
Basically trying to bring all DB calls to a central function, so that I can log them and do error handling at one place.
Assuming a,b,c are just identifiers, you can simply replace
(\w+)\.executeQuery\(\)
with
someClass.executeQueryEx(\1)
or
someClass.executeQueryEx($1)
depending on your regex engine.
If they can be arbitrary expressions, as in foo(quux).bar[25].executeQuery() I don't think you can do that with regexes alone.
I'm having difficulty throwing away the bits of the expression I don't want, and keeping the bits I do.
The problem is - given the input string:
{if cond}foo{else}bar{/if}
I'd like just to have:
0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3: bar
And for the input string:
{if cond}foo{/if}
I'd like just to have:
0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3:
The regex I've got at present looks like this:
\{if ([a-z0-9]+)\}([^\{]*?)(((?:\{else\})?)(.*?)?)\{/if\}
I get the following data back:
0: {if cond}foo{else}bar{/if}
1: cond
2:
3: foo{else}bar
4:
5: foo{else}bar
Which would require further parsing of the foo{else}bar bit to get the respective values.
Is my regex anywhere near close?
I'm not too concerned about the sort of data this might be run over - including { in the body of an if statement is allowed to break the regex. The values of foo and bar can be anything that does not include a {.
Thanks,
Dom
This should work :
{if\s+([^}]*)}([^{]*)(?:{else}([^{]*))?{/if}
Escape it according to your needs
What about this?
\{if ([a-z0-9]+)\}([^\{]*)(?:\{else\})?([^\{]*)\{/if\}
Regex tester. It uses the .NET regex engine, but it might come in handy.
It's note stated in your question, but from the tags it seems that you use the Boost C++ library.
Maybe it is also of interest for you to look at the Boost.Spirit library (included in Boost). Spirit.Qi allows you to parse complex data, while the grammar is looks like EBNF. While the companion Spirit.Karam alows to define the output format, again in EBNF like syntax.
With this library you can generate an AST from the templated document, manipulate it and then generate the output document.
Beside of the documentation of Boost.Spirit, there are some greate slides from 2007 and 2008 wich give a fairly good introduction.