FAQ Database Discussion Community


How do I keep parts of a string that match a regular expression in r? [duplicate]

regex,r,string-matching
This question already has an answer here: Extract a regular expression match in R version 2.10 7 answers If you have a string in r, that has something like... messystuffSample0001moremessystuff and you would like to get Sample0001 What would be a good way of doing that, especially if messystuff,...

Searching a string input for a phrase in Python

python,string-matching
Is there a way in Python to search an input string for a phrase, and then return e.g. a 1 if it is there, or a 0 if not? I want it to work like this: def findphrase(var): if re.compile(r'\b({0})\b'.format(var), flags=re.IGNORECASE).search is True: return 1 else: return 0 def howareyou():...

How to trim substring from string with the condition of being found at the end of it?

php,regex,string,str-replace,string-matching
For instance, this would be the criteria for my condition of trimming: The substring 2 preceeded by a space would be removed from the end of the string, but not if it is found inside the string and before its end. The date is 2015 would remain the same, but...

Find the index of the row in data frame that contain one element in a string vector

r,data.frame,string-matching
If I have a data.frame like this df <- data.frame(col1 = c(letters[1:4],"a"),col2 = 1:5,col3 = letters[10:14]) df col1 col2 col3 1 a 1 j 2 b 2 k 3 c 3 l 4 d 4 m 5 a 5 n I want to get the row indices that contains one...

Fuzzy string matching in r

r,string-matching,fuzzy
I have 2 datasets with more than 100K rows each. I would like to merge them based on fuzzy string matching one column('movie title') as well as using release date. I am providing a sample from both datasets below. dataset-1 itemid userid rating time title release_date 99991 1673 835 3...

Remove part of the string in json document using str replace for many records

regex,r,string-matching,stringr
I would like to replace a string in this file which is causing the invalid json arguments. I can manually delete the first string "_id" : ObjectId( "539163d7bd350003" ), and can convert this json to a data frame. Is there a way I can replace all the instances of json...

turbo pascal remove second space from string

string-matching,turbo-pascal
This seems simple to me, but I can't get my brain around it. I want to take a string, check for spaces, ignore the first space, but remove all subsequent spaces. For example: MyString := 'Alexander The Great'; Output would be 'Alexander TheGreat' Much thanks in advance! (Using Turbo Pascal...

When is the Kleene star of a finite language free?

algorithm,parsing,string-matching,formal-languages,kleene-star
I'm looking for references that give an algorithm to solve this problem: Problem: Given a finite alphabet Σ and a finite language L ⊆ Σ* , determine whether L* is a free monoid. Equivalently, the problem is to determine, given a finite set of strings, whether every concatenation of these...

Java string matching with enum

java,string-matching
I am having an odd issue where an if statement is not evaluating to true when two strings which are seemingly the same are checked. I have the following method : public void render(Cell.Context context, TaskSchedule object, SafeHtmlBuilder sb) { String taskName = getValue(object); System.out.println(taskName); if (String.valueOf(ServerTask.RUN_CYME_EXPORT).equals(taskName)) { taskName =...

R match two lists and find matching elements

r,list,pattern-matching,string-matching
I have two lists: lst1 <- list(c("environmental science", "environmental social science", "nature"), c("bodies of water", "erosion landforms", "valleys"), c("meteorological concepts", "climate", "environmental"), c("fireplaces", "metalworking", "industrial")) lst2 <- list(c("environmental social", "fragile", "ocean"), c("air", "water", "rain water"), c("day", "astronomy")) I want to retain the groupings of list elements, and match the elements...

how to check if string in two different arrays match

php,arrays,string-matching
I would like to check if a string $value in two different arrays match. If they match, do something. If they don't match, do something. Here is the code to get the string value seperately but I would like if possible to combine them together and then compare the strings...

find position of tag inside a string javascript

javascript,jquery,html,regex,string-matching
Thanks in advance. I am stuck with a problem. I have a string like this var string = "This is where <span>I got stuck</span> and I am <span>clueless</span> can anyone <span>help please</span> "; I want to know the position of each occurrence of the span tag in the string. In...

agrep working with del, ins arguments

r,arguments,string-matching,agrep
How can "abteam" with "ab" be matched using this code? agrep("abteam",c("acb","abd","ab"),value=T,ignore.case = TRUE,max = list(del = 10, ins = 10, sub = 10)) The result is character(0), though I specified del=10, ins=10. What is the problem? How does agrep work?...

Ruby: why does =~ evaluate to true in an if statement?

ruby,regex,if-statement,pattern-matching,string-matching
I'm learning Ruby and have seen (and using) regex matching in this way: a = "string9" if a =~ /\d/ #do something end The code would work but today I read the documentation on Regex and learned that =~ returns the position of the match in the string, nil if...

Normalize strings that represent (combinatorical) necklaces [closed]

python,combinatorics,string-comparison,string-matching
I'm trying to match "necklaces" of symbols in Python by looking up their linear representations, for which I use normal strings. For example, the strings "AABC", "ABCA", "BCAA", "CAAB" all represent the same necklace (pictured). In order to get an overview, I store only one of the equivalent strings of...

Exact string matching in r

regex,r,string-matching
I struggling with exact string matching in R. I need only exact match in sentece with searched string: sentence2 <- "laptop is a great product" words2 <- c("top","laptop") I was trying something like this: sub(paste(c("^",words2,"$")),"",sentence2) and I need replace laptop by empty string only - for exact match (laptop) but...

Fuzzy matching movie titles without using a loop and extracting equivalent titles by release date

r,matching,string-matching,levenshtein-distance,fuzzy-search
I am trying to merge two data sets based on movie title column that contains movie names using fuzzy string matching. A sample from the 2 data sets are given below. The first data set looks like itemid userid rating time title release_date 99995 1677 854 3 1997-12-22 sweet nothing...

I have and expression like “ABC or DEF and ( GHI or JKL)” . Now I want to extract all the operands from this expression

jquery,regex,expression,string-matching,string-parsing
ABC or DEF and ( GHI or JKL) Here, the operand ABC can be a phrase which may contain spaces. The only operators are and, or. Can anyone suggest a way to fetch all the operands? I thought to do it this way: There will be a list of operators...

Parsing large strings in python

python,string-matching
I am trying to parse a string extract specific words. The string {{About|the ALGOL-like programming language|the scripting language formerly named Small|Pawn (scripting language)}} '''SMALL''', Small Machine Algol Like Language, is a [[computer programming|programming]] [[programming language|language]] developed by Dr. [[Nevil Brownlee]] of [[Auckland University]]. ==History== The aim of the language was...

How to get almost matching string from Oracle table?

sql,oracle,string-matching,utl-match
I have a table in Oracle with four columns. Now user can enter input string as "operation Knee right" (which is valid) to my query and my query should return the ICD Code (IKR123) which matches most of the word in DiagnosisName column. Following is my current query.(Not giving the...

agrep string matching in R

r,string-matching,tm,agrep,qdap
I have two list of some product names. My problem is "Operating system" is matching with "system", "cooling system",etc. But it has to match only with "Operating","OS". Another example is "Key Board" should be matched with "key" or "KB" but not with "Mother Board" or just "Board". How to give...

KMP Algorithm Table Explanation

string,algorithm,string-matching
In the KMP algorithm we create the patter table having the longest length of the proper prefix that matches proper suffix. Example : char: | a | b | a | b | a | b | c | a | index: | 0 | 1 | 2 | 3...

strpos - user agent partial matching

php,string-matching,strpos
I am trying to detect 2 different phones based on their user agents. I used strpos but my code is outputting "other phone" on both phones. $ua = $_SERVER['HTTP_USER_AGENT']; $phone1 = "SM-G900F"; $phone2 = "GT-I9505"; if(strpos($phone1,$ua)!==false){ echo "gs 5"; } elseif (strpos($phone2,$ua)!==false){ echo "gs 4"; } else { echo "other...

How to match pattern 2.0.0.XXX in lua?

lua,pattern-matching,string-matching
How can I match the pattern 2.0.0.xxx in lua? I want a pattern which can match all such patterns. Example: If a = 2.0.0.100 and b=2.0.0.200 c= 2.1.0.122 d=2.0.1.122 only a and b should match the pattern...

Find numbers after specific text in a string with RegEx

regex,string,powershell,string-matching,powershell-v4.0
I have a multiline string like the following: 2012-15-08 07:04 Bla bla bla blup 2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla 2012-15-08 07:05 Another text that I don't want to search... 2012-15-08 07:06 Another text that I don't want to...

R character match and rank

r,grep,pattern-matching,string-matching
I have a character vector var1 <- c("pine tree", "dense forest", "red fruits", "green fruits", "clean water", "pine") and a list var2 <- list(c("tall tree", "fruits", "star"), c("tree tall", "pine tree", "tree pine", "black forest", "water"), c("apple", "orange", "grapes")) I want to match words in var1 with elements in var2,...

R - stringdist cost setting error

r,string-matching,levenshtein-distance
I have an error when I try to set the operations costs in stringdist Any ideas why ? library(stringdist) seq = rbind( c('aaa'), c('aba'), c('aab'), c('ccc') ) This works perfectly (Levensthein distance) stringdistmatrix(a = seq, b = seq, method = 'lv') When I want to set the costs (substitution twice...

Comparing files line by line using a simple pattern match

regex,perl,pattern-matching,string-matching
I have two files: in the first file each line has some labels associated with it; the second file contains the labels which fall under certain categories. File1 - labelled lines: I have never had an issue. L_102 ----- L_127 I travel overseas and offer a lot of services that...

SQL Alphanumeric Pattern Matching

sql,tsql,sql-server-2012,string-comparison,string-matching
we ran into an issue where we need to test two varchar numeric strings. So if we had one string like '123456' and '123465'. The character could be swapped at any place in the string. I have no clue what to even Google for help with this, but my hope...

Grabbing number from selected class based on string match

javascript,jquery,regex,string-matching
I need to grab the number between [ and ] within the selected class of an li list, and store the number in a variable. I've tried the following, but I'm missing something. I'm not sure of the regex required to look between brackets and grab a string. Javascript var...

How to replace all matching characters except the first occurance

javascript,jquery,regex,string,string-matching
I am trying to use regex to compare a string in javascript. I want to replace all '.'s and '%'s with empty character '' but the catch is I don't want to replace the first occurrence of '.'. value.replace(/\%\./g, ''); Expected result like below: .4.5.6.7. ==> .4567 4.5667.444... ==> 4.56667444...

Rabin Karp Algorithm for 2D arrays

algorithm,string-matching,rabin-karp
How to extend rabin karp to look for an mxm pattern among nxn characters? Can anyone come up with a pseudo code? And Will there be any affect on the time complexity of the algorithm?...

R Match character vectors

r,pattern-matching,string-matching
var1 is a character vector var1 <- c("tax evasion", "all taxes", "payment") and var2 is another character vector var2 <- c("bill", "income tax", "sales taxes") Want to compare var1 and var2 and extract the terms which has a partial word match, for example, the desired answer in this case will...