Questions tagged [match]

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

match
Filter by
Sorted by
Tagged with
1169 votes
15 answers
1.5m views

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 ...
Richard's user avatar
  • 32.3k
861 votes
2 answers
2.4m views

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

Is there a Python equivalent for the switch statement?
John Alley's user avatar
  • 8,787
849 votes
31 answers
560k views

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 ...
user1877442's user avatar
  • 8,681
688 votes
10 answers
420k views

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's user avatar
435 votes
4 answers
805k views

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 ...
Ryan C. Thompson's user avatar
424 votes
10 answers
190k views

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() { ...
Jeroen's user avatar
  • 16k
336 votes
10 answers
384k views

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 준영's user avatar
292 votes
6 answers
379k views

Check if string begins with something? [duplicate]

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 ...
n00b0101's user avatar
  • 6,883
262 votes
4 answers
362k views

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 ...
ari's user avatar
  • 4,421
235 votes
12 answers
196k views

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's user avatar
  • 4,705
161 votes
4 answers
472k views

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 ...
Asda's user avatar
  • 1,635
150 votes
15 answers
510k views

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's user avatar
  • 51.8k
130 votes
2 answers
41k views

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 ...
Henry Henrinson's user avatar
119 votes
8 answers
193k views

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 ...
esqew's user avatar
  • 44.1k
117 votes
7 answers
162k views

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 ...
mesnicka's user avatar
  • 2,538
99 votes
6 answers
107k views

Case insensitive search in Mongo

I am using a case insensitive search in Mongo, something similar to https://stackoverflow.com/q/5500823/1028488. ie. I am using a regex with options i. But I am having trouble restricting the regex to ...
Praneeta's user avatar
  • 1,574
99 votes
10 answers
120k views

How to match an empty dictionary in Javascript?

From the node REPL thing, > d = {} {} > d === {} false > d == {} false Given I have an empty dictionary, how do I make sure it is an empty dictionary ?
João Pinto Jerónimo's user avatar
99 votes
5 answers
146k views

Count number of occurrences of a pattern in a file (even on same line)

When searching for number of occurrences of a string in a file, I generally use: grep pattern file | wc -l However, this only finds one occurrence per line, because of the way grep works. How can I ...
jrdioko's user avatar
  • 32.8k
83 votes
2 answers
66k views

How to use multiple cases in Match (switch in other languages) cases in Python 3.10

I am trying to use multiple cases in a function similar to the one shown below so that I can be able to execute multiple cases using match cases in python 3.10 def sayHi(name): match name: ...
Egidius's user avatar
  • 1,201
82 votes
7 answers
253k views

Delete rows containing specific strings in R

I would like to exclude lines containing a string "REVERSE", but my lines do not match exactly with the word, just contain it. My input data frame: Value Name 55 REVERSE223 22 ...
user3091668's user avatar
  • 2,290
80 votes
2 answers
279k views

Jquery Value match Regex [duplicate]

I am trying to validate the input for E-Mail via JQuery: My JQuery <script> /* <![CDATA[ */ jQuery(function(){ $( ".mail" ).keyup(function() { var VAL = $(this).val(); var email =...
Deproblemify's user avatar
  • 3,400
79 votes
4 answers
71k views

How to pass a variable into regex in jQuery/Javascript

Is there a way to pass a variable into a regex in jQuery/Javascript? I wanna do something like: var variable_regex = "bar"; var some_string = "foobar"; some_string.match(/variable_regex/); In Ruby ...
user avatar
78 votes
4 answers
134k views

Replace whole line when match found with sed

I need to replace the whole line with sed if it matches a pattern. For example if the line is 'one two six three four' and if 'six' is there, then the whole line should be replaced with 'fault'.
irek's user avatar
  • 1,085
69 votes
9 answers
158k views

A method to count occurrences in a list

Is there a simple way to count the number of occurrences of all elements of a list into that same list in C#? Something like this: using System; using System.IO; using System.Text....
Nona Urbiz's user avatar
  • 4,943
61 votes
4 answers
56k views

How to get a button's height to match another element's height?

I want to put a button next to a EditText and I want their heights to match. For example, from the built in Android browser: The Go button is the same height as the EditText field. I know I could ...
cottonBallPaws's user avatar
61 votes
4 answers
76k views

Negation of %in% in R [duplicate]

Is there a short negation of %in% in R like !%in% or %!in%? Of course I can negate c("A", "B") %in% c("B", "C") by !(c("A", "B") %in% c("B", "C")) (cf. this question) but I would prefere a more ...
Qaswed's user avatar
  • 3,789
61 votes
3 answers
147k views

JavaScript error: "val.match is not a function"

I’m using the match function with a Regular Expression. The code I’m using is: if(val.match(/^s+$/) || val == "" ) However, it produces the following error: "val.match is not function&...
zahir's user avatar
  • 611
59 votes
6 answers
276k views

Regex to match only uppercase "words" with some exceptions

I have technical strings as the following: "The thing P1 must connect to the J236 thing in the Foo position." I would like to match with a regular expression those only-in-uppercase words (namely ...
Patrick's user avatar
  • 2,639
59 votes
2 answers
72k views

Can we use match to check the type of a class

I'm new to scala, and I'm learning the match keyword now. I wanna know if we can use the keyword match to check the type of a class. My code is: object Main { def main(args: Array[String]) { ...
Freewind's user avatar
  • 196k
57 votes
4 answers
127k views

using $and with $match in mongodb

I am trying to use the following query in MongoDB but it is not working. db.test.aggregate( $match: { $and: [ type: { $in: ["TOYS"] }, type: { $nin: ["BARBIE&...
Manus's user avatar
  • 879
57 votes
2 answers
70k views

Why can I compare a String to a &str using if, but not when using match?

I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { ...
robfuscator's user avatar
53 votes
5 answers
168k views

regular expression to match exactly 5 digits

testing= testing.match(/(\d{5})/g); I'm reading a full html into variable. From the variable, want to grab out all numbers with the pattern of exactly 5 digits. No need to care of whether before/...
i need help's user avatar
  • 2,386
53 votes
9 answers
82k views

Regex Group in Perl: how to capture elements into array from regex group that matches unknown number of/multiple/variable occurrences from a string?

In Perl, how can I use one regex grouping to capture more than one occurrence that matches it, into several array elements? For example, for a string: var1=100 var2=90 var5=hello var3="a, b, c" var7=...
therobyouknow's user avatar
52 votes
7 answers
99k views

Display only the n'th match of grep

onefish onechicken twofish twochicken twocows threechicken What if I want to grep for lines containing "two", but I only want the 2nd match. So I want the result "twochicken".
Andrew Tsay's user avatar
  • 1,913
47 votes
4 answers
30k views

Can I use '<' and '>' in match?

I am trying to do a simple quadratic function that would return number of roots and their values via an enum: enum QuadraticResult { None, OneRoot(f32), TwoRoots(f32, f32), } fn ...
dawid's user avatar
  • 675
46 votes
3 answers
122k views

Using match to find substrings in strings with only bash

Although I am almost sure this has been covered, I can't seem to find anything specific to this. As I continue my journey on learning bash I keep finding parts where I am baffled as to why things ...
Adesso's user avatar
  • 948
44 votes
6 answers
58k views

Search and replace specific query string parameter value in javascript

I have a string which is something like this : a_href= "www.google.com/test_ref=abc"; I need to search for test_ref=abc in thisabove strinng and replace it with new value var updated_test_ref = "...
TopCoder's user avatar
  • 4,266
42 votes
3 answers
260k views

how to check if string contains '+' character [duplicate]

I want to check if my string contains a + character.I tried following code s= "ddjdjdj+kfkfkf"; if(s.contains ("\\+"){ String parts[] = s.split("\\+); s= parts[0]; // i want to strip part after ...
user93796's user avatar
  • 19.1k
40 votes
7 answers
82k views

Lua need to split at comma

I've googled and I'm just not getting it. Seems like such a simple function, but of course Lua doesn't have it. In Python I would do string = "cat,dog" one, two = string.split(",") and then I would ...
Jaron Bradley's user avatar
40 votes
7 answers
51k views

Javascript Regexp loop all matches

I'm trying to do something similar with stack overflow's rich text editor. Given this text: [Text Example][1] [1][http://www.example.com] I want to loop each [string][int] that is found which I do ...
Tom Gullen's user avatar
39 votes
3 answers
134k views

PHP compare two arrays and get the matched values not the difference

I'm trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, I can't find the right array function to use... I found the array_diff() function: http://php....
Julian Paolo Dayag's user avatar
39 votes
6 answers
72k views

ElasticSearch: How to search for a value in any field, across all types, in one or more indices?

I have two indices my_index_1 and my_index_2. Within these indices, I have the following document types: my_index_1: people organizations roles skills my_index_2: products services patents ...
Information Technology's user avatar
39 votes
6 answers
54k views

grep return 0 if no match

Is there any bash script which can return me a result=true with command grep? Example: There are 1000 records of 103.12.88 in my CF logs. Can I do a grep 103.12.88 if detect 1 or more results then ...
julian lee's user avatar
38 votes
3 answers
9k views

str_ireplace() with keeping of case

How can I use str_ireplace (or something similar) to replace some text for formatting and then return it with the same caps? Example: $original="The quick red fox jumps over the lazy brown dog."; $...
Francisc's user avatar
  • 79k
37 votes
4 answers
37k views

Python- how do I use re to match a whole string [duplicate]

I am validating the text input by a user so that it will only accept letters but not numbers. so far my code works fine when I type in a number (e.g. 56), it warns me that I should only type letters ...
Thomas's user avatar
  • 1,198
37 votes
2 answers
60k views

Pattern matching field names with jq

This is a very basic (probably silly) question but I can't get it to work... I have a JSON file with this structure: { "data": { "what a burger": [1,2,3], "wap": [66], "...
manu's user avatar
  • 1,383
36 votes
6 answers
55k views

Is there a way to check if two object contain the same values in each of their variables in python?

How do I check if two instances of a class FooBar(object): __init__(self, param): self.param = param self.param_2 = self.function_2(param) self.param_3 = self.function_3() ...
Aufwind's user avatar
  • 25.8k
36 votes
3 answers
132k views

MySQL SELECT query string matching

Normally, when querying a database with SELECT, its common to want to find the records that match a given search string. For example: SELECT * FROM customers WHERE name LIKE '%Bob Smith%'; That ...
JR Lawhorne's user avatar
  • 3,242
35 votes
1 answer
43k views

delete lines with sed match a special regex

I try to delete all lines that begin with some optional special chars followed by blubb: That's the lines I want to match: #blubb *blubb -blubb blubb That should do it, but doesn't work :( sed "...
Thomas's user avatar
  • 1,203
34 votes
4 answers
67k views

Why won't re.groups() give me anything for my one correctly-matched group?

When I run this code: print re.search(r'1', '1').groups() I get a result of (). However, .group(0) gives me the match. Shouldn't groups() give me something containing the match?
dtc's user avatar
  • 1,814

1
2 3 4 5
176