JSR223 reg ex does not work with \\ - jmeter

in JSR223 I am able to remove whatever is between
<Notes> and </Notes>:
<Notes>{
"abc":12,
"xyz": "Success",
}</Notes>
with this regex -->
str1 = str1.replaceAll("<Notes[^>]*>([^<]*)<\\/Notes>", "<Notes><\\/Notes>");
but when i have: -->
<Notes>{
"abc":D:\\xyz\\12.txt,
"xyz": "Success",
}</Notes>
that reg ex does not work because now I have \ to worry about and not sure how to incorporate that.

How do you know it "doesn't work"? It should work for whatever is between Notes tags
So it might be the case you are incorrectly referring the result or it is been overwritten somewhere else.
You can try the following things just in case:
You don't need to escape / in the regex.
You can try amending your regex to something like <Notes>[\\s\\S]*?</Notes>
Make sure you're using Groovy language

Related

Is there a way I can print a statement with an exclamation mark and a quotation mark?

I'm trying to print a statement that looks like this: "Oh no!",
but I keep getting: Oh no! without the quotes.
This is the code I've been using:
print(exclamation.capitalize() + ("!") , "I yelled" + ".")
Please what am I missing?
In Python 3.6+, you can use f-strings to embed statements directly in the string.
Also, if you use single quotes to capture the string, you can use double-quotes in the string and they will print without needing to be escaped.
print(f'"{exclamation.capitalize()}"! I yelled.')

How to fix "mapping values are not allowed in this context " error in yaml file?

I've browsed similar questions and believe i've applied all that i've been able to glean from answers.
I have a .yml file where as far as I can tell each element is formatted identically. And yet according to YamlLint.com
(<unknown>): mapping values are not allowed in this context at line 119 column 16
In this case, line 119 is the line containing the second instance the word "transitions" below. That I can tell each element is formatted identically. Am I missing something here?
landingPage:
include: false
transitions:
-
condition:location
nextState:location
location:
include:false
transitions:
-
condition:excluded
nextState:excluded
excluded:
include:false
transitions:
-
condition:excluded
nextState: excluded
-
condition:age
nextState:age
You cannot have a multiline plain scalar, such as your include:false transitions be the key to a mapping, that is why you get the mapping values not allowed in this context error.
Either you forgot that you have to have a space after the value indicator (:), and you meant to do:
include: false
transitions:
or you need to quote your multi-line scalar:
'include:false
transitions':
or you need to put that plain scalar on one line:
include:false transitions:
please note that some libraries do not allow value indicators in a plain scalar at all, even if they are not followed by space
I fixed this for myself by simply realizing I had indented a line too far, and un-indenting it.
we need to use space before ":"
Then it will excecute
check the yaml script in below
http://www.yamllint.com/
There are couple of issues in the yaml file, with yaml files it gets messy, fortunately it can be identified easily with tools like yaml lint
Install it
npm install -g yaml-lint
Here is how you can validate
E:\githubRepos\prometheus-sql-exporter-usage\etc>yamllint prometheus.yaml
√ YAML Lint successful.
For me the problem was a unicode '-' from a cut and paste. Visualy it looked OK, but the character was 'EN DASH' (U+2013) instead of 'HYPHEN MINUS' (U+002D)
In mine case it was the space after the : in a value:
query-url: https://blabla.com/blabla?label=blabla: blabla
To fix:
query-url: https://blabla.com/blabla?label=blabla:%20blabla
Or:
query-url: "https://blabla.com/blabla?label=blabla: blabla"
If you are using powershell and have copied the cat command, it won't work properly (I'm guessing it is encoding the content in some way). Instead of using "$(cat file.yaml)" you should use $(Get-Content file.yaml -Raw) without the quotes.
Really annoying!
In my case if was some odd disappearing of the initial formatting of the initial chart that was copied in Intellij Idea. It was possible to gfigure out with text-compare tool only:
So, when you do your copy and paste in your IDE, please double check is what you have copied is exactly what you paste, aren't some additional spaces were added.

Why is rubocop asking me to put // around regex when i'm using %r already?

I have the following regex
regexp = %r{
((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)
}x
But when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
You can disable (and enable) any rubocop cop by adding a .rubocop.yml file to the root of your project folder and setting up the appropriate configurations. To see what you can do, check out the global default.yml in your rubocop package. It's fully commented.
For this particular problem, create a .rubocop.yml and...
To disable the cop completely:
Style/RegexpLiteral:
Enabled: false
To always use %r:
Style/RegexpLiteral:
EnforcedStyle: percent_r
I don't run rubocop so not sure this will solve your problem. You can use // instead of {} to surround the regex when using %r:
regexp = %r/((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x
You can use multiline regexp with /.../x either:
regexp = /
((returned|undelivered)
\s
mail|mail
\s
delivery
(\sfailed)?)
/x
See more in Rubocop gem doc
when I run rubocop on it, it complains that I need to "Use // around regular expression."
How can I get around it?
I think the message is pretty clear: to get around it, you can use // around the regular expression:
regexp = /((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)/x

Single quote string interpolation to access a file in linux

How do I make the parameter file of the method sound become the file name of the .fifo >extension using single quotes? I've searched up and down, and tried many different >approaches, but I think I need a new set of eyes on this one.
def sound(file)
#cli.stream_audio('audio\file.fifo')
end
Alright so I finally got it working, might not be the correct way but this seemed to do the trick. First thing, there may have been some white space interfering with my file parameter. Then I used the File.join option that I saw posted here by a few different people.
I used a bit of each of the answers really, and this is how it came out:
def sound(file)
file = file.strip
file = File.join('audio/',"#{file}.fifo")
#cli.stream_audio(file) if File.exist? file
end
Works like a charm! :D
Ruby interpolation requires that you use double quotes.
Is there a reason you need to use single quotes?
def sound(FILE)
#cli.stream_audio("audio/#{FILE}.fifo")
end
As Charles Caldwell stated in his comment, the best way to get cross-platform file paths to work correctly would be to use File.join. Using that, your method would look like this:
def sound(FILE)
#cli.stream_audio(File.join("audio", "#{FILE}.fifo"))
end
Your problem is with your usage of file path separators. You are using a \. Whereas this may not seem like a big deal, it actually is when used in Ruby strings.
When you use \ in a single quoted string, nothing happens. It is evaluated as-is:
puts 'Hello\tWorld' #=> Hello\tWorld
Notice what happens when we use double quotes:
puts "Hello\tWorld" #=> "Hello World"
The \t got interpreted as a tab. That's because, much like how Ruby will interpolate #{} code in a double quote, it will also interpret \n or \t into a new line or tab. So when it sees "audio\file.fifo" it is actually seeing "audio" with a \f and "ile.fifo". It then determines that \f means 'form feed' and adds it to your string. Here is a list of escape sequences. It is for C++ but it works across most languages.
As #sawa pointed out, if your escape sequence does not exist (for instance \y) then it will just remove the \ and leave the 'y'.
"audio\yourfile.fifo" #=> audioyourfile.fifo
There are three possible solutions:
Use a forward slash:
"audio/#{file}.fifo"
The forward slash will be interpreted as a file path separator when passed to the system. I do most my work on Windows which uses \ but using / in my code is perfectly fine.
Use \\:
"audio\\#{file}.fifo"
Using a double \\ escapes the \ and causes it to be read as you intended it.
Use File.join:
File.join("audio", "#{file}.fifo")
This will output the parameters with whatever file separator is setup as in the File::SEPARATOR constant.

Replace specific characters between brackets in ruby

I have a string
str = "'${1:textbox}',[${2:x},${3:y},${4:w},${5:h}]"
and I would like to replace all , between [ and ] with a single space.
I have attempted to use something like
str.gsub!(/(?<=\[)\,*?(?=\])/," ")
without success. However, if I replace \, in my expression with ., I get the expected output:
str.gsub!(/(?<=\[).*?(?=\])/," ")
== "'${1:textbox}',[ ]"
Could someone please explain the proper regex technique to use in this situation, and perhaps also explain why the examples I have posted above have failed and succeeded?
I am using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]
It may be possible to do this with a single regex, but even if it is, I can guarantee it'll be ugly beyond description. It's a lot simpler to use "nested" substitution - use one gsub to find bracketed substrings, and then use another to swap out the commas:
str.gsub(/\[.*?\]/) do |substr|
substr.gsub(',', ' ')
end
I'm afraid I can't explain why your attempts have failed - neither of them would run for me (ruby 1.8.7 / irb 0.9.5). IRB gave errors that vaguely said "Bad regexp syntax." And I can't quite grok how they're supposed to work (edit: mu is too short has an awesome breakdown in his answer - check that out). Hope this is helpful anyway!
This regex:
/(?<=\[)\,*?(?=\])/
is looking for an opening bracket followed by a sequence of commas (of any length) followed by a closing bracket. That means things like this:
[]
[,]
[,,,,,,,,,,,]
Your string doesn't look like that so your first gsub! doesn't do anything. If you do this:
'[,,,,,,]'.gsub(/(?<=\[),*?(?=\])/, " ")
You'll get a '[ ]' for your troubles.
Your second regex:
/(?<=\[).*?(?=\])/
works because .*? matches anything (subject to newlines and /m and /s modifiers of course) and the portion of your string between [ and ] certainly qualifies as anything.
If you're trying to produce this:
"'${1:textbox}',[${2:x} ${3:y} ${4:w} ${5:h}]"
then I'd go with Xavier Holt's nested gsub approach, that's simple and clean.

Resources