我的病媒包括“ZZ1Z01Z0Z0Z0”等条目“1001Z0Z0Z0Z0Z0Z0Z0Z0”,等等。
- The third character is a Z
- The third AND seventh characters are Z
- The third AND seventh characters are Z, AND none of the other characters are Z
我试图用大刀和灰色玩.,但我无法根据地势限制我的条件。 任何建议?
感谢!
我的病媒包括“ZZ1Z01Z0Z0Z0”等条目“1001Z0Z0Z0Z0Z0Z0Z0Z0”,等等。
我试图用大刀和灰色玩.,但我无法根据地势限制我的条件。 任何建议?
感谢!
您可以定期表达(见?regexp
,详见常规表述)。
<代码>grep 回到配对处的位置,如果找不到配对器,则将充其量。 您不妨使用<代码>grepl,因为它使你能够使用的一种合乎逻辑的病媒分崩离析。
z <- c("ZZZ1Z01Z0ZZ0", "1001ZZ0Z00Z0")
# 3rd character is Z ("^" is start of string, "." is any character)
grep("^..Z", z)
# 3rd and 7th characters are Z
grep("^..Z...Z", z)
# 3rd and 7th characters are Z, no other characters are Z
# "[]" defines a "character class" and "^" in a character class negates the match
# "{n}" repeats the preceding match n times, "+" repeats is one or more times
grep("^[^Z]{2}Z[^Z]{3}Z[^Z]+", z)
扩大对口的回答,你希望
your_dataset <- data.frame(
z = c("ZZZ1Z01Z0ZZ0", "1001ZZ0Z00Z0")
)
regexes <- c("^..Z", "^..Z...Z", "^[^Z]{2}Z[^Z]{3}Z[^Z]+")
lapply(regexes, function(rx)
{
subset(your_dataset, grepl(rx, z))
})
还考虑将<代码>grepl(rx,z)改为str_detect(z, rx)
,使用<编码>stringr。 (除略为可读代码外,没有实际差异。)
如果你想要的话,你可以做头两件事,而没有使用子指挥的定期表达,以删除具体特征。
# Grab the third character in each element and compare it to Z
substr(z, 3, 3) == "Z"
# Check if the 3rd and 7th characters are both Z
(substr(z, 3, 3) == "Z") & (substr(z, 7, 7) == "Z")
然而,乔舒亚的定期表达方式更为灵活,试图实施你采用分位办法的第三项限制将是一种痛苦。 经常表达方式更适合像你第三次限制这样的问题,学习如何使用这些言论从来不是坏的想法。
I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...
The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I tried to print all the possible combination of members of several vectors. Why the function below doesn t return the string as I expected? #include <iostream> #include <vector> #...
I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...
I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??
Is there a PHP string function that transforms a multi-line string into a single-line string? I m getting some data back from an API that contains multiple lines. For example: <p>Some Data</...
I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...