Extract sentence containing a word from text [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a large text. I need find any word in the text then copy to variable the phrase that contains the word. How can I do this by C#? Maybe I can use some regular expression?

Use the regex expression [^.!?;]*(search)[^.?!;]*[.?!;], where "search" is your query.
string query = "professional";
var regex = new Regex(string.Format("[^.!?;]*({0})[^.?!;]*[.?!;]", query));
string text =
#"Stack Overflow is a question and answer site for professional and enthusiast programmers.
It's built and run by you as part of the Stack Exchange network of Q&A sites.
With your help, we're working together to build a library of detailed answers to
every question about programming.";
var results = regex.Matches(text);
for (int i = 0; i < results.Count; i++)
Console.WriteLine(results[i].Value.Trim());
This code uses the regex to find all sentences containing "professional", and outputs them, trimming any whitespace.
Output: Stack Overflow is a question and answer site for professional and enthusiast programmers.

Related

Regex to remove c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to remove some string from regex in c#.
I have a string
000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip
From this I want to remove those strings
140702
142110
And I want a result as
000603ABC-005051-I-FILL200-NNYYNY180319--A2002.zip
How can I do that in regex?
You can use the Regex.Replace method (https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx).
var regex = new Regex("(140702)|(142110)");
var result = regex.Replace("000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip", "");
Although, as already stated by #CompuChip, String.Replace would probably be more efficient in this case. There's a discussion of various methods of replacing substrings and their efficiency at https://blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance/.

Regular expression to search for files and folders [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Help write a regular expression to search for files and folders,
searches for a given mask. In the mask, you can use "*"
(any characters in any number), and the "?" (one symbol).
Here shows you how to use regex in C#.
You could always just loop the directory that you're looking in and check the file names instead of making a regex. (You'll need to use System.IO)
Perhaps something like this?
string [] fileEntries = Directory.GetFiles(targetDirectory);
Regex regex = new Regex("target file name");
Match match = regex.Match(string.Join(" ", fileEntries););
if (match.Success)
{
Console.WriteLine(match.Value);
}

C# Regex questions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I extract the word "Word" (or any word) using Regex into a string in both cases? in the following string:
Word = CreateObject(980, -1696.5996, -12.2998, 5.3, 0, 0, 317.999);
MoveObject(Word, -1660.9004, 79.40039, 2.2,6);
I am pretty new in Regex, and I am having hard time understanding the tutorials over the internet.
I have tried using /(.*)( = CreateObject)/ but no luck
Tried using /MoveObject\(.*?\,/ and it works, but it matches MoveObject too, and I want it to match only "Word"
Thanks!
You could try the below regex.
#".*?(?= = CreateObject)|(?<=MoveObject\().*?(?=,)"
OR
#"\S+?(?= = CreateObject)|(?<=MoveObject\().*?(?=,)"
DEMO

C# Using regex, how to find a language in url [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a class library to add a customized toolbox to a external IE browser.
I'm using SHDocVw to do the back/forward buttons (e. g.). My question is, sense this is multilingual, how can I change the buttons text...
The url give me "/en/" or "/ar/", how can i catch this?
Thanks
With the Mati Cicero answer I managed to do it.
string page = ie.LocationURL;
Regex rgx = new Regex(#"https?://[a-zA-Z0-9.-_]+(/((?'lang'ar|en)|.*))/?");
var lang = rgx.Match(page).Success ? rgx.Match(page).Groups[1].Value : "ar";
You can try the following regex, I tried to make it as much generic as I could:
https?:\/\/[a-zA-Z0-9\.\-_]+(\/((?'lang'es|en)|.*))\/?
You would then examine the group "lang" to see if a match was made.
You would aso like to add more available languages (Added ch and in):
https?:\/\/[a-zA-Z0-9\.\-_]+(\/((?'lang'es|en|ch|in)|.*))\/?
If its always the first directory;
var lang = new Uri("http://sitename.com/en/pages/default.aspx")
.Segments[1].Replace("/", string.Empty).ToLower();

C# subtraction of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am sorry for that bad Title, but basicaly my problem is really simple. I got 1 string which is basic alphabet and the second string which is gonna be part of the alphabet (8 characters) which user will fill up by himself. If 2 characters are the same, they will get removed and then rest of characters will be in the TextBox3. could someone pls help me ?
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = TextBox2.Text;
I assume you want to check the existence of substring and remove from the parent string, Try this
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = textBox2.Text;
if (alphabet.ToLowerInvariant().Contains(special))
{
textBox3.Text = alphabet.Replace(special, "");
}

Categories