Case matching with R's gsub? [duplicate] - regex

This question already has answers here:
How do I replace the string exactly using gsub()
(1 answer)
Matching entire string in R
(3 answers)
Closed 9 years ago.
I am trying to write a code in R to change all "non" word to "none" in a string but I don't want to change any other words, specially I don't want to change "none" to "nonee".
I tried this code:
gsub("non","none", "non none", fixed = TRUE)
but result is:
"none nonee"
Is there anyway to do this using R's gsub?

You can use word boundaries...
x <- c("non" , "none")
gsub( "\\bnon\\b" , "none" , x )
#[1] "none" "none"

Related

Scala substring match to match noun [duplicate]

This question already has answers here:
How to compare a string with another where the one has space in between
(4 answers)
Closed 4 years ago.
Is there any regex to match substring if they have space in between in scala ?
For eg:
"hero 6 go pro" contains "gopro" should return true
"go pro hero 6 " contains "gopro" should return true
I tried :
def matchWords(input: Seq[Char], words: Seq[Char]): Boolean = (input, words) match {
case (Seq(), Seq() | Seq(' ', _*)) => true
case (Seq(), _) => false
case (Seq(a, tail#_*), Seq(b, rest#_*)) if a == b => matchWords(tail, rest)
case (_, Seq(' ', rest#_*)) => matchWords(input, rest)
case _ => false
}
but
matchWords("gopro", "hero 6 go pro") returns false
though this matchWords("fitbit", "fit bit versa") return true.
The string should match nouns.
Any idea what I am doing wrong here ?
Thanks,
Shalini
A user with the same "name" as you has already asked a very similar question here and been given multiple answers, including one from me.
The code in your question appears to be copied from another of those answers. Unfortunately you picked the lowest-scoring answer which doesn't give the right results.
I suggest that you try my answer from that question...

Reg exp in java for alphabets,numbers and comma [duplicate]

This question already has answers here:
Regular expression for letters, numbers and - _
(6 answers)
Difference between * and + regex
(7 answers)
Closed 4 years ago.
I want a regular expression in java which should allow alphabets,number and comma. Could anyone please help?
I am using the code. But it is not working
Pattern p = Pattern.compile("^[A-Za-z0-9,]$", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(object.toString());
boolean bcheck = m.find();

Remove Bracket [*TESTABC*] along with content from String prefix in java [duplicate]

This question already has answers here:
Java replace all square brackets in a string
(8 answers)
Closed 4 years ago.
I am having string as below and want prefix to be removed [*TESTABC*]
String a = "[*TESTABC*]test#test.com";
Expected result: test#test.com
I tried below code but it is removing the bracklets only. I need to remove the inner content also.
The string with come in this pattern only. Please help.
String s = "[*TESTABC*]test#test.com";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s); //*TESTABC*test#test.com
int index;
String s = "[*TESTABC*]test#test.com";
index=s.indexOf(']');
s=s.substring(index+1);
System.out.println(s);
**i solve your question according to your problem and output **

Python: replace Japanese word with brace, such as {keyword: 部屋}, {keyword: 公園}, ..... with 'keyword' [duplicate]

This question already has answers here:
Match text between two strings with regular expression
(3 answers)
Closed 4 years ago.
there are some Japanese sentences like following:
{keyword: 部屋}いいね!
{keyword: 公園}は綺麗です.
私は{keyword: 部屋捜査}です。
   ..........
.........
I want to replace the substring like :{keyword: 部屋},{keyword: 公園}..... with 'keyword'.
For example:
input: 私は{keyword: 部屋捜査}です
output: 私はkeywordです
My trying code is following and but it is wrong, the result is same:
import re
s = '{keyword: 賃貸}'
t = re.sub(r"\{keyword:[あ-んア-ン一-]+\}", 'keyword', s)
print(t)
Thanks!
Use the following:-
inputString = "私は{keyword: 部屋捜査}です"
t = re.sub(r"\{keyword:[^}]*}", 'keyword', inputString)
print(t)

R regex remove all punctuation except apostrophe [duplicate]

This question already has answers here:
Remove all punctuation except apostrophes in R
(4 answers)
Closed 9 years ago.
I'm trying to remove all punctuation from a string except apostrophes. Here's my exastr2 <-
str2 <- "this doesn't not have an apostrophe,.!##$%^&*()"
gsub("[[:punct:,^\\']]"," ", str2 )
# [1] "this doesn't not have an apostrophe,.!##$%^&*()"
What am I doing wrong?
A "negative lookahead assertion" can be used to remove from consideration any apostrophes, before they are even tested for being punctuation characters.
gsub("(?!')[[:punct:]]", "", str2, perl=TRUE)
# [1] "this doesn't not have an apostrophe"
I am not sure if you can specify all punctuations except ' within a regexp the way you've done. I would check for alphanumerics + ' + space with negation:
gsub("[^'[:lower:] ]", "", str2) # per Joshua's comment
# [1] "this doesn't not have an apostrophe"
You could use:
str2 <- "this doesn't not have an apostrophe,.!##$%^&*()"
library(qdap)
strip(str2, apostrophe.remove = FALSE, lower.case = FALSE)