How to jump to/after Nth comma [duplicate] - unix

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the most efficient way to reach a spot on sight on VIM?
With the code like this:
[caret here]create_stage :pay_deposit, due: deposit_due_date, actual: deposit_paid_date, action: 'Deposit', status: status
I want to jump right to/before/after the Nth comma (or at least around) of actual: deposit_paid, [need to be here], action: 'etc'
What is the most efficient way of doing it? (I currently just w-w-w which sucks, also could start counting number of words to use something like 12w but that just distracts too much).
I don't want search since I do want to keep current search and highlighting.

You can combine the f motion with a [count], so for your example, 3f, would be the shortest way. (And if you have miscounted, you can correct with ; and ,.)

f,;;; or use easymotion and probable dupe of What is the most efficient way to reach a spot on sight on VIM?

What about searching the comma character?
/, and then input a number, say 3 and then type n or N to locate all commas in the doc?

Related

Extracting text between symbols, over multiple lines [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 2 years ago.
I have XML files of parlament protocols, of which I want to extract all of the interruptions mentioned. The interruptions are marked by brackets - like this:
Text I don't care about.
(applause from the right)
Text I don't care about.
I was given this code, which seemed to work just fine:
files <- as.list(dir(pattern = ".xml"))
my_list <- lapply(files, function(x) xmlToList(xmlParse(x)))
my_list2 <- lapply(my_list, function(x) enframe(regmatches(x[["TEXT"]],
gregexpr("(?=\\().*?(?<=\\))", x[["TEXT"]], perl=T))[[1]])
Like this I only got the (applause from the right), but now I realised, that this code apparently only considers text per line and I have some interruptions over multiple lines (1 - 3), like this
Text I don't care about.
(applause from the right and
from the left)
Text I don't care about.
If the interruption is in this format, I get no results. How do I have to change the gregexpr to look for one line, but also for multiple lines, until the corresponding ")" is found? I've been trying \n but so far no luck.
Thanks in advance
Edit
To further explain myself: I am looking at multiple hundreds of protocols (each one has its own XML file), each with multiple hundreds of these interruptions. So I am more specifically looking for a solution to extract them all with the same code.
A solution close to the code I used before would be extra helpful, since I am still fairly new to R.
Here is one way.
Sample2 = "Text I don't care about.
(applause from the right and
from the left)
Text I don't care about."
sub(".*\\((.*?)\\).*", "\\1", Sample2)
[1] "applause from the right and\n from the left"

COUNTIF of non-empty and non-blank cells

In Google Sheets I want to count the number of cells in a range (C4:U4) that are non-empty and non-blank. Counting non-empty is easy with COUNTIF. The tricky issue seems to be that I want to treat cells with one or more blank as empty. (My users keep leaving blanks in cells which are not visible and I waste a lot of time cleaning them up.)
=COUNTIF(C4:U4,"<>") treats a cell with one or more blanks as non-empty and counts it. I've also tried =COUNTA(C4:U4) but that suffers from the same problem of counting cells with one or more blanks.
I found a solution in stackoverflow flagged as a solution by 95 people but it doesn't work for cells with blanks.
After much reading I have come up with a fancy formula:
=COUNTIF(FILTER(C4:U4,TRIM(C4:U4)>="-"),"<>")
The idea is that the TRIM removes leading and trailing blanks before FILTER tests the cell to be greater than or equal to a hyphen (the lowest order of printable characters I could find). The FILTER function then returns an array to the COUNTIF function which only contains non-empty and non-blank cells. COUNTIF then tests against "<>"
This works (or at least "seems" to work) but I was wondering if I've missed something really obvious. Surely the problem of hidden blanks is very common and has been around since the dawn of excel and google sheets. there must be a simpler way.
(My first question so apologies for any breaches of forum rules.)
I don't know about Google. But for Excel you could use this array formula for multiple contiguous columns:
=ROWS(A1:B10) * COLUMNS(A1:B10)-(COUNT(IF(ISERROR(CODE(A1:B10)),1,""))+COUNT(IF(CODE(A1:B10)=32,1,"")))
Could try this but I'm not at all sure about it
=SUMPRODUCT(--(trim((substitute(A2:A5,char(160),"")))<>""))
seems in Google Sheets that you've got to put char(160) to match a space entered into a cell?
Seems this is due to a non-breaking space and could possibly apply to Excel also - as explained here - the suggestion is that you could also pass it through the CLEAN function to eliminate invisible characters with codes in range 0-31.
I found another way to do it using:
=ARRAYFORMULA(SUM(IF(TRIM($C4:$U4)<>"",1,0)))
I'm still looking for a simpler way to do it if one is available.
This should work:
=countif(C4:U4,">""")
I found this solution here:
Is COUNTA counting blank (empty) cells in new Google spreadsheets?
Please let me know if it does.
=COLUMNS(C4:U4)-COUNTBLANK(C4:U4)
This will count how many cells are in your range (C4 to U4 = 19 cells), and subtract those that are truly "empty".
Blank spaces will not get counted by COUNTBLANK, despite its name, which should really be COUNTEMPTY.

R decompose a list [duplicate]

This question already has answers here:
Assign multiple new variables on LHS in a single line
(15 answers)
Closed 9 years ago.
In python it is possible to decompose a list
x=[1,2,3]
a,b,c=x # a=1 b=2 c=3
is it possible to do something similar in R?
for example something like:
x=matrix(rnorm(100),10,10)
[u d v]=svd(x) # instead of u=svd$u d=svd$d v=svd$v
Not without some hackery and fiddly global assignment, and my personal opinion is that this is not a good thing to do.
Why? Well, if the results from svd (to use your example) are returned together there's a good reason they are returned together - they belong together. Once you break it up you lose that relationship. The only win is having fewer characters to type, and that's not one of the things we optimise with programming - readability should win over that.

Password strength via regular express? [duplicate]

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
at least 8 characters long
include at least one upper case letter
one lower case letter
one number
try this..
^((?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,})
You could use something like that:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,}$
Test it here.
You may also want to learn about the "?=" thing, which is called "positive lookahead" here.
In short, when all three lookaheads (.*\d and .*[a-z] and .*[A-Z]) are matched (and are discarded), the main regex [a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,} can be matched too.
Do you have to do this in one regex? I would make each of those rules one regex, and test for them individually. I suspect you code will end up being simpler, and you'll save yourself and whoever has to maintain your application several headaches.

Unnecessary asterisk in regex that finds CSS comment

I thought to ask this as an update to my previous similar question but it became too long.
I was trying to understand a regex given in w3.org that matches css comments and got this doubt
Why do they use
\/\*[^*]*\*+([^/*][^*]*\*+)*\/
----------------^
instead of just
\/\*[^*]*\*+([^/][^*]*\*+)*\/
?
Both are working similarly. Why do they have an extra star there?
Let's look at this part:
\*+([^/*][^*]*\*+)*
-A- --B-- -C-
Regex engine will parse the A part and match all the stars until there is NO MORE stars or there is a line break. So once A is done, the next character must be a line break or anything else that's not a star. Then why instead of using [^/] they used [^/*]?
Also look at the repeating capturing group.
([any one char that's not / or *][zero or more chars that's not *][one or more stars])
It captures groups of characters ending with atleast one or more stars. So C will take all the stars leaving B with no stars to match in the next round.
So the B part won't get a chance to meet any stars at all. That is why I think there's no need to put a star there.
But that regex is in w3.org so I guess my understanding may be wrong. Please explain what I'm missing.
This has already been corrected in the CSS3 Syntax module:
\/\*[^*]*\*+([^/][^*]*\*+)*\/ /* ignore comments */
Notice that the extraneous asterisk is gone, making this expression identical to what you have.
So it would seem that it was simply a mistake on their part while writing the grammar for CSS2. I'm digging the mailing list archives to see if there's any discussion there that could be relevant.

Resources