Match with blank and without blank - c#

I want or get the name of mp3
I'm currently using this code
string str = "onClick=\"playVideo('upload/honour-3.mp3',this)\"/> onClick=\"playVideo('upload/honor is my honor .mp3',this)\"/> onClick=\"playVideo('upload/honour-6.mp3',this)\"/> ";
string Pattern = #"playVideo\(\'upload\/(?<mp3>\S*).mp3\'\,this\)";
if (Regex.IsMatch(str, Pattern))
{
MatchCollection Matches = Regex.Matches(str, Pattern);
foreach (Match match in Matches)
{
string fn = match.Groups["mp3"].Value;
Debug.Log(match.Groups["mp3"].Value);
}
}
But \ S * matches only like
honour-3
honour-6
i can't get "honor is my honor "
i try the"\S*\s*",but it not work
I have a lot of how many blank string uncertain
How do I use Regex to get mp3's name?

If you dont have to match "playVideo" and "upload", Your regex is unnecessarily complicated. This one produces the expected results:
#"[\w\s-]+\.mp3"
Results:
"honour-3.mp3",
"honor is my honor .mp3",
"honour-6.mp3"
If you don't want .mp3 at the end of the matches, you can change the regex to #"([\w\s-]+)\.mp3" and select the second group (the first one is the whole match).
Regex.Matches(str, #"([\w\s-]+)\.mp3").Cast<Match>().Select(m => m.Groups[1].Value).ToArray();
Results:
"honour-3",
"honor is my honor ",
"honour-6"

Related

Find hashtags in string

I am working on a Xamarin.Forms PCL project in C# and would like to detect all the hashtags.
I tried splitting at spaces and checking if the word begins with an # but the problem is if the post contains two spaces like "Hello #World Test" it would lose that the double space
string body = "Example string with a #hashtag in it";
string newbody = "";
foreach (var word in body.Split(' '))
{
if (word.StartsWith("#"))
newbody += "[" + word + "]";
newbody += word;
}
Goal output:
Example string with a [#hashtag] in it
I also only want it to have A-Z a-z 0-9 and _ stopping at any other character
Test #H3ll0_W0rld$%Test => Test [#H3ll0_W0rld]$%Test
Other Stack questions try to detect the string and extract it, I would like it work with it and put it back in the string without losing anything that methods such as splitting by certain characters would lose.
You can use Regex with #\w+ and $&
Explanation
# matches the character # literally (case sensitive)
\w+ matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$& Includes a copy of the entire match in the replacement string.
Example
var input = "asdads sdfdsf #burgers, #rabbits dsfsdfds #sdf #dfgdfg";
var regex = new Regex(#"#\w+");
var matches = regex.Matches(input);
foreach (var match in matches)
{
Console.WriteLine(match);
}
or
var result = regex.Replace(input, "[$&]" );
Console.WriteLine(result);
Ouput
#burgers
#rabbits
#sdf
#dfgdfg
asdads sdfdsf [#burgers], [#rabbits] dsfsdfds [#sdf] [#dfgdfg]
Updated Demo here
Another Example
Use a regular expression: \#\w*
string pattern = "\#\w*";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);

Looking for patterns in a string how to?

I'm trying to find all instances of the substring EnemyType('XXXX') where XXXX is an arbitrary string and the instasnce of EnemyType('XXXX') can appear multiple times.
Right now I'm using a consortium of index of/substring functions in C# but would like to know if there's a cleaner way of doing it?
Use regex. Example:
using System.Text.RegularExpressions;
var inputString = " EnemyType('1234')abcdeEnemyType('5678')xyz";
var regex = new Regex(#"EnemyType\('\d{4}'\)");
var matches = regex.Matches(inputString);
foreach (Match i in matches)
{
Console.WriteLine(i.Value);
}
It will print:
EnemyType('1234')
EnemyType('5678')
The pattern to match is #"EnemyType\('\d{4}'\)", where \d{4} means 4 numeric characters (0-9). The parentheses are escaped with backslash.
Edit: Since you only want the string inside quotes, not the whole string, you can use named groups instead.
var inputString = " EnemyType('1234')abcdeEnemyType('5678')xyz";
var regex = new Regex(#"EnemyType\('(?<id>[^']+)'\)");
var matches = regex.Matches(inputString);
foreach (Match i in matches)
{
Console.WriteLine(i.Groups["id"].Value);
}
Now it prints:
1234
5678
Regex is a really nice tool for parsing strings. If you often parse strings, regex can make life so much easier.

Process regex matches and non-matches differently

When the following code is run:
string input = "<td>abc</td><td></td><td>abc</td>)";
string pattern = "<td>(abc)?</td>";
foreach (Match match in Regex.Matches(input, pattern))
Console.Write(match.Groups[1].Value);
If outputs the following text:
abcabc
That makes sense since the pattern only matches the first and the last td elements in the input string. However, I'd like to change it so that it outputs the following:
abc
abc
In other words, I'd like it to output a new line when it encounters an empty td element. How could I accomplish this?
You could do that like this:
string input = "<td>abc</td><td></td><td>abc</td>)";
string pattern = "<td>(abc)?</td>";
foreach (Match match in Regex.Matches(input, pattern))
{
if (match.Groups[1].Success)
Console.Write(match.Groups[1].Value);
else
Console.WriteLine();
}
By changing your pattern from <td>(abc)</td> to <td>(abc)?</td>, the abc becomes optional. In other words, either <td>abc</td> or <td></td> inputs will match. Since the entire group is optional, you can then use the Group.Success property to determine whether or not the capturing group exists in each match.

Match with reges excluding string tags

I am trying to write a code in order to get the matches in a list but without the match tags, until now i have built the following code in WP7 application written on C#
public static MatchCollection MatchTags(string content, string string_start, string string_end)
{
MatchCollection matches = Regex.Matches(content, string_start + "(.*?)" + string_end, RegexOptions.IgnoreCase);
return matches;
}
So how to return matches without string_start, string_end (match tags) without use of replace function after the match extraction?
Use lookarounds..
String.Format("(?<={0}).*?(?={1})",string_start,string_end);
Though you can also use groups.i.e in your regex (.*?) would capture the content within Group 1.No need of lookarounds then..
MatchTags(content,start,end).Cast<Match>()
.Select(x=>x.Groups[1].Value);
It works when i get the result with the next code:
string my_string_no_tags = matches[number].Groups[1].Value;
Consider the following code...
MatchCollection matches = Regex.Matches(content, string.Format("(?<={0}).*?(?={1})", string_start, string_end), RegexOptions.IgnoreCase);
return matches;
Good Luck!

Regex to match and return group names

I need to match the following strings and returns the values as groups:
abctic
abctac
xyztic
xyztac
ghhtic
ghhtac
Pattern is wrote with grouping is as follows:
(?<arch>[abc,xyz,ghh])(?<flavor>[tic,tac]$)
The above returns only parts of group names. (meaning match is not correct).
If I use * in each sub pattern instead of $ at the end, groups are correct, but that would mean that abcticff will also match.
Please let me know what my correct regex should be.
Your pattern is incorrect because a pipe symbol | is used to specify alternate matches, not a comma in brackets as you were using, i.e., [x,y].
Your pattern should be: ^(?<arch>abc|xyz|ghh)(?<flavor>tic|tac)$
The ^ and $ metacharacters ensures the string matches from start to end. If you need to match text in a larger string you could replace them with \b to match on a word boundary.
Try this approach:
string[] inputs = { "abctic", "abctac", "xyztic", "xyztac", "ghhtic", "ghhtac" };
string pattern = #"^(?<arch>abc|xyz|ghh)(?<flavor>tic|tac)$";
foreach (var input in inputs)
{
var match = Regex.Match(input, pattern);
if (match.Success)
{
Console.WriteLine("Arch: {0} - Flavor: {1}",
match.Groups["arch"].Value,
match.Groups["flavor"].Value);
}
else
Console.WriteLine("No match for: " + input);
}

Categories