Regex: Remove everything except emoticons. how? [duplicate] - c#

This question already has answers here:
How to match emoticons with regular expressions?
(3 answers)
Closed 9 years ago.
If I have string such as "I love my country :) :D. I like myself :P -_- .", how to remove everything except Emoticons - so the resulting string should be without any text ?
Input String or text can be any type.
I am using Regex
Regex.Replace(str, "[A-Za-z]", "");
but it also remove "P""D" in ":D :P" smiley. What will be the Regex then?
Thanks in advance.

There is a lot of emoticons so so you wil. end with a very long and over complicated regular expression but. In this case I think you only care of Two 'corrupted' emoticons after the replace. So if this is the case, this should work:
[ABCE-OQ-Za-oq-z]|(?<!:)D|(?<!:)[Pp]
This regular expresssion match on ABC, the range from E to O and then the ragne of Q to Z for lowercase letters it matches from a to o and from q to z. The key part in the regular expression is that it only matches D, P and p if the matched char is not preceded by a colon. This feature is called lookaround (or in this exact use case lookbehind).

Related

Regex: match string with parathese with '|' operator [duplicate]

This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 1 year ago.
I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.
Individually,
'Number' can match the word number
'Number[(]s[)]' can match Number(s).
However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".
What I have tried:
1: Put \b boundary around the second string, doesn't work.
2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option
I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.
Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it.
Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form.
Also the pattern should be: Number\(s\)|Number
The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".

regex in C# not working properly for numeric check [duplicate]

This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Closed 2 years ago.
I tried to use regexes in C#
^(?=.*\d)(?=.*[a - z])(?=.*[A - Z])(?=.*[!##$%^*])(?=.*[a-zA-Z]).{6,20}$
but \d comes as an error if i put [0-9] instead it wont work as desired
This should check the string has a uppercase, lowercase, symbol and a number
You should use [0-9]. Probably it is more correct... \d will catch non-european digits like рен (it is a Devanagari digit).
For the reason:
you probably wrote:
var rx = new Regex("\d");
But in this way the \d is an escape sequence of the string instead of being a regex.
Write
var rx = new Regex(#"\d");
to deactivate the escape expansion of strings.

How to get all possible Regex Matches [duplicate]

This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV

Not terminated set of [] in regex (C#) [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 5 years ago.
I'm trying to parse a text looking for data inside this pattern:
{{([^]+)}}
i.e. any sequence of characters between {{ and }} .
But, when I try to build a Regex object:
Regex _regex = new Regex("{{([^]+)}}", RegexOptions.Compiled);
I got this error:
analysis of "{{([^]+)}}" - Set of [] not terminated....
whatever it means...
Someone has an hint?
The purpose of [^...] is to negate character classes present in the specified list. After the ^ symbol, in order to define a correct regular expression, you should include a set of characters to exclude like, for example [^a]+ (this matches one or more characters that don't include the literal a).
The regex you are attempting to define is probably:
{{\s*([\w]+)\s*}}
Visit this link for trying a working demo.
This is because [^] is not a valid regex, because you need to specify at least one symbol that you wish to exclude.
In order to capture the string up to the closing }} change the expression to this:
{{((?:[^}]|}[^}])*)}}
Demo.

Finding character which is not in double quote using Regular Expression [duplicate]

This question already has answers here:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I am new in writing regular expression and I have the following Scenario.
I have a string, like :
string line = "if (true){var data = string.Format(\"something {0} {1}.\", \"is\", \"wrong\");}";
now I need to write a regular expression that just pick the closing curly braces which are not in the double quote
so far I tried this:
"(^(\"[^\"]*\")(}))+"
^(\"[^\"]*\") : I want to Ignore any substring which is inside double quote, AND
(}) : I want to take }
+: for at least 1 occurrence.
But it seems I Did something wrong. Could any one please guide me to sort out where I did the wrong?
Thank you.
You just need these parts of your regex:
(?:\"[^\"]*\")|(})
Regex live here.

Categories