Get only word before special char
Tag : chash , By : Marc Dong
Date : March 29 2020, 07:55 AM
seems to work fine I have the file with "chunked" sentences from medical sector. , This seem to work: var patt = @"\s(\b(.+?))/";
var matches = Regex.Matches("[ADVP again/RB ] [VP seen/VBN ] [NP is/VBZ ] [NP a/DT focal/JJ asymmetry/NN ].", patt);
var matchedValues = matches
.Cast<Match>()
.Select(match => match.Groups[1].Value);
var output = string.Join(" ", matchedValues);
|
How to detect a special char in string and modify this special word?
Tag : chash , By : user135518
Date : March 29 2020, 07:55 AM
will help you I have a string, it may looks like this: , Perhaps simply: str = string.Join(" ", str.Split()
.Select(w => w.StartsWith("@") ? "%" + w + "%" : w));
@abc. --> wrong: %@abc.% correct: %@abc%.
static readonly HashSet<char> WordSeparators = new HashSet<char> { ',', '.', '!', '?', ';', ':', ' ', '-', '/', '\\', '[', ']', '(', ')', '<', '>', '"', '\'' };
static string WrapWithIfStartsWith(string input, string startsWith, string wrap, StringComparison comparison = StringComparison.CurrentCulture)
{
if (string.IsNullOrEmpty(input) || !input.StartsWith(startsWith, comparison))
return input;
else if(input.Length == 1)
return string.Format("{1}{0}{1}", input, wrap);
char first = input.First();
char last = input.Last();
bool firstIsSeparator = WordSeparators.Contains(first);
bool lastIsSeparator = WordSeparators.Contains(last);
if (firstIsSeparator && lastIsSeparator)
return string.Format("{0}{1}{2}{1}{3}",
first, wrap, input.Substring(1, input.Length - 2), last);
else if (firstIsSeparator && !lastIsSeparator)
return string.Format("{0}{1}{2}{1}",
first, wrap, input.Substring(1));
else if (!firstIsSeparator && lastIsSeparator)
return string.Format("{0}{1}{0}{2}",
wrap, input.Remove(input.Length - 1), last);
else
return string.Format("{1}{0}{1}", input, wrap);
}
string str = "blabalbal blabal @abc.";
str = string.Join(" ", str.Split().Select(w => WrapWithIfStartsWith(w,"@","%")));
|
Remove special char out of a word
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am trying to count the word frequency in a long string. I have split the string into list of words using string.split() method and remove case sensity by applying string.lower() before splitting the long string. I want to remove some special character such as '!', ':', '.' as those character will mess up the word count. Below is the function that I wrote but it seems not to work properly , Use enumerate: def clean_word(word):
replace_list = [':','.',',','!','?']
s = list(word)
for i, x in enumerate(s):
if x in replace_list:
s[i] = ""
word = ''.join(s)
return word
print(clean_word('Hello!'))
# Hello
word = 'Hello!'
replace_list = [':','.',',','!','?']
print(''.join([x for x in word if x not in replace_list]))
# Hello
|
Regex Python Adding a char before a random word and the special char :
Date : January 03 2021, 08:18 AM
like below fixes the issue You can use r'\b' to search for word breaks. For your case you are looking for substrings that match: [A-Za-z\-]+ and are surrounded by word breaks: \b[A-Za-z\-]+\b and are followed by a colon: \b[A-Za-z\-]+\b: You can capture the word using parenthesis: \b([A-Za-z\-]+)\b: and recover it in the substitution using \1 import re
s = 'cat: monkey, ab4 / 1997 / little: cat, 1954/ afgt22 /dog: monkey, 173 / pine-apple: duer, 129378s. / 12'
re.sub(r'(\b[A-Za-z\-]+\b):', r'|\1:', s)
# returns:
'|cat: monkey, ab4 / 1997 / |little: cat, 1954/ afgt22 /|dog: monkey, 173 / |pine-apple: duer, 129378s. / 12'
|
vim - why will search find it but search and replace not? (this escaped special char pattern)
Tag : linux , By : DonMac
Date : October 03 2020, 01:00 AM
help you fix your problem The substitution operation needs to be prefixed with %s and not the other way around as s%. So doing %s/\/scripts\/monitor\/raid_status_mail\.log/\$LOGFILE/g
printf '%s\n' "%s/\/scripts\/monitor\/raid_status_mail\.log/\$LOGFILE/g" w q | ex -s file
|