Spring Expression Language equivalent for \r, \n, \t etc - spring

I am using Spring Integration. I get a string (payload) like below:
<Element>
<Sub-Element>5</Sub-Element>
</Element>
I need to test if above string starts with <Element><Sub-Element> which is actually <Element>\r\n <Sub-Element>.
<int:recipient-list-router id="customRouter" input-channel="routingChannel">
<int:recipient channel="channel1" selector-expression="payload.startsWith('<Element><Sub-Element>')"/>
<int:recipient channel="channel2" selector-expression="!payload.startsWith('<Element><Sub-Element>')"/>
</int:recipient-list-router>
Ideally the first router should pass the test but in this case its failing. Can anyone help me finding out what is the SpEL equivalent of \r \n etc ?

SpEL doesn't have escapes for those, but you can use regular expressions to do the selection...
<recipient selector-expression="payload matches '<Element>\r\n<Sub-Element>.*'" channel="channel1"/>
<recipient selector-expression="!(payload matches '<Element>\r\n<Sub-Element>.*')" channel="channel2"/>
If you are not familiar with regex, the .* at the end matches anything (hence this regex is the equivalent of startsWith()).
EDIT:
While this will work, I feel I should point out that relying on specific values in insignificant white space in XML documents is brittle - if the client changes to use, say \n instead, or even no whitespace, your application will break. You should consider using something like an <int-xml:xpath-router/> instead.

Thanks Gary.
So the working list-recipient-router looks like
Either
<recipient selector-expression="payload matches '(?s)<Element>(\s*)<Sub>(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)<Element>(\s*)<Sub>(.*)')" channel="channel2"/>
Or
<recipient selector-expression="payload matches '(?s)<Element>(.*)<Sub>(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)<Element>(.*)<Sub>(.*)')" channel="channel2"/>
May keep captures () or may not. Both works.

Related

What is the difference between these three alternative ways to write Ruby regular expressions?

I want to match the path "/". I've tried the following alternatives, and the first two do match, but I don't know why the third doesn't:
/\A\/\z/.match("/") # <MatchData "/">
"/\A\/\z/".match("/") # <MatchData "/">
Regexp.new("/\A\/\z/").match("/") # nil
What's going on here? Why are they different?
The first snippet is the only correct one.
The second example is... misleading. That string literal "/\A\/\z/" is, obviously, not a regex. It's a string. Strings have #match method which converts its argument to a regexp (if not already one) and match against it. So, in this example, it's '/' that is the regular expression, and it matches a forward slash found in the other string.
The third line is completely broken: don't need the surrounding slashes there, they are part of regex literal, which you didn't use. Also use single quoted strings, not double quoted (which try to interpret escape sequences like \A)
Regexp.new('\A/\z').match("/") # => #<MatchData "/">
And, of course, none of the above is needed if you just want to check if a string consists of only one forward slash. Just use the equality check in this case.
s == '/'

Ruby gsub/regex to find all chars but not specific words

My Objective:
I have a string like so:
"O_1324||T_6789||EC_67889&&(IC_12345||chicken)||true&&false"
My dream is to use a gsub regex to identify [a-zA-z0-9_] and replace them with something ("false" if you must know). However I don't want to replace the words "true" or "false".
What have I tried
I have been using the super friendly Rubular with little success.
I can get all the "words" (non operators) like so:
(\w+)
I tried matching all the "words" except "true" like so:
(?!true)(\w+)
This did not work. It unmatches only the "t" in true.
You can use following regex :
\b(?:(?!true|false)\b)\w+\b
see demo https://regex101.com/r/eX6rE6/1
Note that you need to use word boundary for matching words. and put the negative look-ahead before \w+ not after!

Ruby/Rails: How do I allow whitespace, dash and ÄÖÜ in my regex?

Currently my regular Expression looks like this: /\A[a-zA-Z]{2,50}\z/i
Now I would like to add acceptance of: -, whitespace and ÄÖÜ.
I tried rubular.com but I'm a total regex noob.
Probably you want to think about using Unicode properties and scripts. You could write your regex then as
\A[\p{L}\s-]{2,50}\z
See it here on Rubular
\p{L} is a Unicode property and matching any letter in any language.
If you want to match ÄÖÜ you maybe also want ß, using Unicode properties you don't have to think about such things.
If you want to limit the possible letters a bit you can use a Unicode script such as Latin
\A[\p{Latin}\s-]{2,50}\z
See it on Rubular
Just add it in the regex as follows:
/\A[a-zA-Z\-\sÄÖÜ]{2,50}\z/i
DEMO
regex is an invaluable cross-language tool that the sooner you learn, the better off you will be. I suggest putting in the time to learn it
In a regex, whitespace is represented by the shorthand character class \s.
A hyphen/minus is a special character, so must be escaped by a backslash \-
Ä, Ö and Ü are normal characters, so you can just add them as they are.
/\A[a-zA-Z\s\-ÄÖÜ]{2,50}\z/i

Regular expression help to skip first occurrence of a special character while allowing for later special chars but no whitespace

I'm looking for words starting with a hashtag: "#yolo"
My regex for this was very simple: /#\w+/
This worked fine until I hit words that ended with a question mark: "#yolo?".
I updated my regex to allow for words and any non whitespace character as well: /#[\w\S]*/.
The problem is I sometimes need to pull a match from a word starting with two '#' characters, up until whitespace, that may contain a special character in it or at the end of the word (which I need to capture).
Example:
"##yolo?"
And I would like to end up with:
"#yolo?"
Note: the regular expressions are for Ruby.
P.S. I'm testing these out here: http://rubular.com/
Maybe this would work
#(#?[\S]+)
What about
#[^#\s]+
\w is a subset of ^\s (i.e. \S) so you don't need both. Also, I assume you don't want any more #s in the match, so we use [^#\s] which negates both whitespace and # characters.

ruby regex match any character besides a specific one

I am looking for a way to match any character besides, for example, a "#."
It would look something like...
gsub(/^foo.*foo$/)
But I'd want it to match
"foofdfdfdfoo"
But not
"fooddgdgd#fdfoo"
Thanks.
^[^#]+$
http://rubular.com/r/glijo99dU9
gsub is for substitution. If you just want to match, the .match method
To expand on Explosion Pills answer, a caret (^) will negate the match in a regex. This means that it will not match if the characters following it are found in the expression. You can read more about it in the documentation.

Resources