Questions tagged [match]

A programming concept about finding results based on some kind of search. Typically used when talking about regular expressions.

A programming concept about finding results based on some kind of search. Typically used when talking about regular expressions.

See also , ,

8645 questions
1138
votes
15 answers

Check whether a string matches a regex in JS

I want to use JavaScript (I can also use jQuery) to do check whether a string matches the regex ^([a-z0-9]{5,})$, and get a true or false result. match() seems to check whether part of a string matches a regex, not the whole thing. Does it solve the…
Richard
  • 31,629
  • 29
  • 108
  • 145
837
votes
31 answers

How to test multiple variables for equality against a single value?

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0: …
user1877442
  • 8,561
  • 3
  • 13
  • 5
789
votes
2 answers

What is the Python equivalent for a case/switch statement?

Is there a Python equivalent for the switch statement?
John Alley
  • 8,067
  • 3
  • 14
  • 10
675
votes
10 answers

What is the difference between re.search and re.match?

What is the difference between the search() and match() functions in the Python re module? I've read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
424
votes
4 answers

Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively inefficient. Is there a more direct way to do it? For…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
399
votes
8 answers

How to match a String against string literals?

I'm trying to figure out how to match a String in Rust. I initially tried matching like this, but I figured out Rust cannot implicitly cast from std::string::String to &str. fn main() { let stringthing = String::from("c"); match…
Jeroen
  • 15,257
  • 12
  • 59
  • 102
326
votes
10 answers

How can I add a string to the end of each line in Vim?

I want to add * to the end of each line in Vim. I tried the code unsuccessfully :%s/\n/*\n/g
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
291
votes
6 answers

Check if string begins with something?

I know that I can do like ^= to see if an id starts with something, and I tried using that for this, but it didn't work. Basically, I'm retrieving a URL and I want to set a class for an element for path names that start in a certain…
n00b0101
  • 6,863
  • 17
  • 42
  • 36
248
votes
4 answers

How to test if a string contains one of the substrings in a list, in pandas?

Is there any function that would be the equivalent of a combination of df.isin() and df[col].str.contains()? For example, say I have the series s = pd.Series(['cat','hat','dog','fog','pet']), and I want to find all places where s contains any of…
ari
  • 4,269
  • 5
  • 24
  • 33
229
votes
12 answers

Return positions of a regex match() in Javascript?

Is there a way to retrieve the (starting) character positions inside a string of the results of a regex match() in Javascript?
stagas
  • 4,607
  • 3
  • 28
  • 28
154
votes
4 answers

Selecting data frame rows based on partial string match in a column

I want to select rows from a data frame based on partial match of a string in a column, e.g. column 'x' contains the string "hsa". Using sqldf - if it had a like syntax - I would do something like: select * from <> where x like 'hsa'. Unfortunately,…
Asda
  • 1,565
  • 2
  • 11
  • 4
150
votes
14 answers

Java: method to get position of a match in a String?

String match = "hello"; String text = "0123456789hello0123456789"; int position = getPosition(match, text); // should be 10, is there such a method?
hhh
  • 50,788
  • 62
  • 179
  • 282
129
votes
2 answers

Why does pattern matching in Scala not work with variables?

Take the following function: def fMatch(s: String) = { s match { case "a" => println("It was a") case _ => println("It was something else") } } This pattern matches nicely: scala> fMatch("a") It was a scala> fMatch("b") It…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
116
votes
7 answers

JavaScript - Use variable in string match

I found several similar questions, but it did not help me. So I have this problem: var xxx = "victoria"; var yyy = "i"; alert(xxx.match(yyy/g).length); I don't know how to pass variable in match command. Please help. Thank you.
mesnicka
  • 2,458
  • 8
  • 23
  • 31
115
votes
7 answers

What does the "Nothing to repeat" error mean when using a regex in javascript?

I'm new to Regex and I'm trying to work it into one of my new projects to see if I can learn it and add it to my repitoire of skills. However, I'm hitting a roadblock here. I'm trying to see if the user's input has illegal characters in it by using…
esqew
  • 42,425
  • 27
  • 92
  • 132
1
2 3
99 100