Regex_extract the next (and only the first) line after string - ruby

Using regex (on Ruby), how can I extract one line (only next line) after strings, like this
Title:
this is the text I'd like to extract
Not this one
Neither this
I managed to extract the text using "Lookahead and Lookbehind" like this:
puts text.scan(/Title:[^;]*)Not this one/)
but the second part ("Not this one") is not always mentionned

^(?<=Title:\n)([^\n]+$)
DEMO
Check it please.

Related

How do I ignore comments in a text file with LabVIEW?

I've created a script file reader, nothing more than a glorified text reader that changes loop cases in my program, but I need it to be able to ignore comments on a line, execute that command, and go to the next line and process the new command after it finds the comment denoted with a semicolon. For the life of me, I can't figure out how to do this.
Currently, the commands are read in like this:
DO THIS FUNCTION
DO THAT FUNCTION
I'd like to comment it with a semicolon like this:
DO THIS FUNCTION ;this is a comment to be ignored
Below is my text file read code, should be able to drag and drop it in to test. The command indicator just echoes the command being read. I've removed the rest of my program, sorry, can't send that part.
Can someone shed some light?
Is a semicolon used anywhere else in your file? Or is it just used to indicate a comment?
If it is only used to indicate a comment then as you read each line in, call the Split String primitive and split at the ";". Just use the top output regardless of whether or not the line contains a semicolon:
You can use the "Match Regular Expression Function" to split up the string, as #Moray already suggested.
Sadly I can't give you an example vi right now.
The main idea is:
find the "Match Regular Expression Function"
give it a ; as char to search for
there are three outputs of the function (before match, match, after match)
use the 'before match' instead of the whole line and give it to the rest of your program
This only works if your commands don't contain any ; except for the comments.
Note: I not quite sure what happens if you give the function a string that doesn't contain ; but you can figure that out by yourself by using the detailed help to this function :)

Need to strip out invalid characters in CSV file

I am generating a CSV file from a Microsoft SQL database that was provided to me, but somehow there are invalid characters in about two dozen places throughout the text (there are many thousands of lines of data). When I open the CSV in my text editor, they display as red, upside-down question marks (there are two of them in the attached screenshot).
When I copy the character and view the "find/replace" dialog in my text editor, I see this:
\x{0D}
...but I have no idea what that means. I need to modify my script that generates the CSV so it strips these characters out, but I don't know how to identify them. My script is written in Classic ASP.
You can also use RegEx to remove unwanted characters:
Set objRegEx = CreateObject(“VBScript.RegExp”)
objRegEx.Global = True
objRegEx.Pattern = “[^A-Za-z]”
strCSV = objRegEx.Replace(strCSV, “”)
This code is from the following article which explains in details what it does:
How Can I Remove All the Non-Alphabetic Characters in a String?
In your case you will want to add some characters to the Pattern:
^[a-zA-Z0-9!##$&()\\-`.+,/\"]*$
You can simply use the Replace function and specify Chr(191) (or "¿" directly):
Replace(yourCSV, Chr(191), "")
or
Replace(yourCSV, "¿", "")
This will remove the character. If you need to replace it with something else, change the last parameter from "" to a different value ("-" for example).
In general, you can use charmap.exe (Character Map) from Run menu, select Arial, find a symbol and copy it to the clipboard. You can then check its value using Asc("¿"), this will return the ASCII code to use with Chr().

Parsing between quotes in Ruby

I'm trying to parse a string, but I'm running into trouble:
str = " #Var(\"^This is the text I want to save.\") "
What I want to parse out is: ^This is the text I want to save. Basically anything that's between the quotes.
I was trying to use a str.split()... but I'm unsure about what to specify.
Thanks
You can use regex. Here:
puts str.scan(/"(.*)"/)[0][0]
# ^This is the text I want to save.
The above regex basically looks for everything that is between " and " and returns them.
This should work - assuming you don't expect there to be multiple quoted sections. The 1 specifies the match group you want.
puts str[/"(.*)"/, 1]
# ^This is the text I want to save.
If you want to get array of arrays of different size, you may do it in two steps: .split and .scan. In your case .scan has () on two sides of |, that's why you have trouble with nil (Which supposed to be useful, but not it your case). So you have either use .flatten.compact or add the 3rd step of .delete.
Try this:
text.split("\n").map{|i|p i.scan(/'([^']+)'|(\w+)/).flatten.compact}
text.split("\n").map{|i|p i.scan(/'[^']+'|\w+/).map{|i|i.delete "'"}}

return line of strings between two strings in a ruby variable

I would like to extract a line of strings but am having difficulties using the correct RegEx. Any help would be appreciated.
String to extract: KSEA 122053Z 21008KT 10SM FEW020 SCT250 17/08 A3044 RMK AO2 SLP313 T01720083 50005
For Some reason StackOverflow wont let me cut and paste the XML data here since it includes "<>" characters. Basically I am trying to extract data between "raw_text" ... "/raw_text" from a xml that will always be formatted like the following: http://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=PHNL%20KSEA
However, the Station name, in this case "KSEA" will not always be the same. It will change based on user input into a search variable.
Thanks In advance
if I can assume that every strings that you want starts with KSEA, then the answer would be:
.*(KSEA.*?)KSEA.*
using ? would let .* match as less as possible.

After finding all the element matches a specified xpath, how to I get them using indexing?

When I do /table/tr (it contains all tr elements in a table), I can get the first one by using /table/tr[1], get the second one using /table/tr[2]
how about when I do
//tr[contains(., "some text here")
(assume there are more than 1 tr contains text "some text here"
how do I retrieve the first one? second one .....
Thanks!
You should continue using indexing as you've described:
//tr[contains(., "some text here")][1]

Resources