Это, кажется, работает
import pandas as pd
df = pd.DataFrame({'A': ['1--->3', '4', '6', ''], 'B': ['2', '4', '--->8', '4'], 'C': ['5', '0', '4', '2--->']})
print(df)
print('~~~~~~~~~~~~~~~~~~~~~~~')
filtered_df = df[(df['A'].str.contains('--->')) | (df['B'].str.contains('--->')) | (df['C'].str.contains('--->'))]
print(filtered_df)
выход
A B C
0 1--->3 2 5
1 4 4 0
2 6 --->8 4
3 4 2--->
~~~~~~~~~~~~~~~~~~~~~~~
A B C
0 1--->3 2 5
2 6 --->8 4
3 4 2--->
/^([^,]*,){21}[^,]*$/
То есть:
^ Start of string
( Start of group
[^,]* Any character except comma, zero or more times
, A comma
){21} End and repeat the group 21 times
[^,]* Any character except comma, zero or more times again
$ End of string
Ровно 21 запятая:
^([^,]*,){21}[^,]$
Не менее 21 запятая:
^([^,]?,){21}.*$
если точно 21:
/^[^,]*(,[^,]*){21}$/
если не менее 21:
/(,[^,]*){21}/
Однако я бы посоветовал не использовать регулярное выражение для такой простой задачи. Потому что это медленно.
.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,
На каком языке? Вероятно, есть более простой метод.
Например ...
В CFML вы можете просто увидеть, равно ли ListLen (MyString)
22
В Java вы можете сравнить MyString .split (',')
до 22
и т. д.
Might be faster and more understandable to iterate through the string, count the number of commas found and then compare it to 21.
Если вы используете разновидность регулярных выражений, поддерживающую квантификатор Possessive (например, Java), вы можете:
^(?:[^,]*+,){21}[^,]*+$
квантификатор Possessive может быть лучше, чем квантификатор Greedy.
Объяснение:
(?x) # enables comments, so this whole block can be used in a regex.
^ # start of string
(?: # start non-capturing group
[^,]*+ # as many non-commas as possible, but none required
, # a comma
) # end non-capturing group
{21} # 21 of previous entity (i.e. the group)
[^,]*+ # as many non-commas as possible, but none required
$ # end of string
var valid = ((" " + input + " ").split(",").length == 22);
или ...
var valid = 21 == (function(input){
var ret = 0;
for (var i=0; i<input.length; i++)
if (input.substr(i,1) == ",")
ret++;
return ret
})();
Будет работать лучше, чем ...
var valid = (/^([^,]*,){21}[^,]*$/).test(input);