I have a string variable wich contain only one letter like "D" or "A" etc ...
I want to replace this letter by an explicit text but when I do (in this case "$F{Action}" contain "D"):
$F{Action}.replace('D','Apple').replace('A','text')
my result is "textpple" Because Apple beggin with an "A" and my second replace is on the letter "A"
How can I do to only replace the letter by the firt replace statment and not do the others replace statement?
There is no need of scriptlet for this small work. You can try using the below expression
$F{Action}.contains( "D" ) ? "Apple" : $F{Action}.contains( "A" ) ? "text" : ""
Hope this is your requirement.
Related
I have to validate if the word contains first letter as "A" second as "R" third as "T". I want to validate it as user is typing so want to validate it character by character. Is there a way in regex?
Something like this should work: ^(A|AR|ART|ART.+)$. Only "A", "AR", "ART", and "ART..." are valid. Though, there's probably a shorter/better way to do it.
You can try this regex !
^(A|AR|ART.*)$
^ represents that the starting character should be like 'A' or 'AR' or 'ART' .The pipe '|' shows that the conditional that multiple regex are possible and '$' signs indicate the end of the line .
I test it through regex 101 & attached is the output !
Is it possible to replace "this" with "that" and "This" with "That" in one regex?
Perl extensions and other special tricks are allowed.
Edit: It's too easy if the replacement starts with the same letter. How about server to node and Server to Node
Assuming I only care about the first letter and using Perl, I came up with:
s/(s)erver/($1 eq uc $1 ? "N" : "n") . "ode"/ie
Yeah, you can do it like:
var z = "This this";
z.replace(/([Tt])his/g, '$1hat');
result would be: That that
I'm trying to get a vector of all the function names in the base package that contain only a . as punctuation, or no punctuation at all. I'd like to do it using only the ls() function.
ls() takes a pattern argument that is defined as
an optional regular expression. Only names matching pattern are returned. glob2rx can be used to convert wildcard patterns to regular expressions.
I'm trying to invert my regular expression. But I also want to keep the functions that contain .. Here's an example of some of the ones I don't want.
lsBase1 <- ls("package:base", pattern = "[[:punct:]]")
head(lsBase1)
# [1] "^" "~" "<" "<<-" "<=" "<-"
I want the inverted version of this, as if I was using invert = TRUE in grep, or by doing the following. But I also want the functions that contain only . if they contain punctuation.
lsBase2 <- ls("package:base")
lsBase2 <- lsBase[!grepl("[[:punct:]]", lsBase)]
head(lsBase2)
# [1] "abbreviate" "abs" "acos" "acosh"
# [5] "addNA" "addTaskCallback"
Is there a way to invert the pattern argument in ls()? Or, more generally can I invert the regular expression [[:punct:]] so it returns the opposite, but includes those matches that contain only . as punctuation?
Note: More than one . is fine.
Another example of what I want is: Yes I want is.vector but no I don't want [.data.frame.
I believe this is what you are looking for:
m <- ls("package:base", pattern="^(\\.|[^[:punct:]])*$")
The | is regex for "OR", so in words, it says something like "match a sequence of characters, running from the start of the string to its end, each of which is either a ., OR not a punctuation character".
To confirm that this works:
## Dissolve the matched strings and check for any verboten characters.
sort(unique(unlist(strsplit(m, ""))))
# [1] "." "0" "1" "2" "3" "4" "8" "a" "A" "b" "B" "c" "C" "d" "D" "e"
# [17] "E" "f" "F" "g" "G" "h" "H" "i" "I" "j" "J" "k" "K" "l" "L" "m"
# [33] "M" "n" "N" "o" "O" "p" "P" "q" "Q" "r" "R" "s" "S" "t" "T" "u"
# [49] "U" "v" "V" "w" "W" "x" "X" "y" "Y" "z"
## Have a look at (at least a few of) the names _excluded_ by the regex:
n <- setdiff(ls("package:base"), m)
sample(n, 10)
# [1] "names<-.POSIXlt" "[[<-.data.frame" "!.hexmode" "$<-"
# [5] "<-" "&&" "%*%" "package_version"
# [9] "$" "regmatches<-"
The following will work for what you are asking.
> lsBase2[grepl('^([^\\pP\\pS]|\\.)+$', lsBase2, perl=T)]
Edit: Or you could simply use the following (R version 3.1.1) returns 1029 results on this:
> ls("package:base", pattern="^[a-zA-Z0-9.]+$")
This is easy if you think about it in steps. First remove the . characters, then scan for additional punctuation:
lsBase2[!grepl('[[:punct:]]', gsub('[.]', '', lsBase2))]
I want to find from a list of strings (all captial letters) that contains "NAME", but before and after the name I don't want any characters. So I tried this regex "[^A-Z]NAME[^A-Z]. But the string that are like "NAME", or "NAME " can not be matched, I thought the [^A-Z] just check as long they are not in these character, and nothing would also be OK. Did I miss something here?
Chris
Try using a word boundry:
\bNAME\b
Here's a demo: http://regexr.com?33f1d
Anyone know why this is happening:
Filename: 031\_Lobby.jpg
RegExp: (\d+)\_(.*)[^\_e|\_i]\.jpg
Replacement: \1\_\2\_i.jpg
That produces this:
031\_Lobb\_i.jpg
For some reason it's chopping the last character from the second back-
reference (the "y" in "Lobby". It doesn't do that when I remove the [^_e|_i] so I must be doing something wrong that's related to that.
Thanks!
You force it to chop off the last character with this part of your regex:
[^_e|_i]
Which translates as: Any single character except "_", "e", "|", "i".
The "y" in "Lobby" matches this criterion.
You mean "not _e" and "not _i", obviously, but that's not the way to express it. This would be right:
(\d+)_(.+)(?<!_[ei])\.jpg
Note that the dot needs to be escaped in regular expressions.
it is removing the "y" because [^_e|_i] matches the y, and the .* matches everything before the y.
You're forcing it to have a last character different from _e and _i. You should use this instead (note the last *):
(\d+)_(.*)[^_e|_i]*.jpg