C# Extract Words Beginning with %! and Ending With !% [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 7 years ago.
Improve this question
I need to extract some words out of a paragraph of text if the word starts with %! and ends with !%
I'd imagine regex would be good for this but unfortunately my regex isn't that great...OK its pretty bad...OK its non existent :(.
EXAMPLE TEXT
You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%
EXPECTED RESULT
beef, girl, Downtown
How can I achieve this in C# with or without regex?

Like this:
var reg = new Regex(#"%!(?<word>\w+)!%");
var inStr = #"You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%";
var results = reg.Matches(inStr).Cast<Match>().Select(m => m.Groups["word"].Value);
This will give you a list of matched words. Converting it to a comma-separated string is an exercise I'll leave up to you..
Also, next time you should probably do some quick research, you're eventually going to have to learn simple regexes..

Related

Generate skipable list based on chars [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 3 years ago.
Improve this question
I am very sure that there is a technical term for this problem, but unfortunately I do not know it.
I have an alphabetical charset and the requirement is to create the combination of all the chars with a maximum length
The idea is (sample):
Generate a collection of A, AA, AAA, AAAA, AAAAA, AAAAAA
Next: A, AB, ABA, ABAA, ABAAA
Next A, AB, ABB, ABBA, ABBAA
The reason:
We have to query an API that delivers search results.
But if I don't get search hits from the API on AAA, I don't need to search for AAAA anymore, because it can't get search hits either. I can then move on to AAB.
The question:
My problem is that I'm not sure how the code has to be built to achieve this goal. I lack the structural approach.
I've already tried nested loops, but unfortunately I don't get the result.
I also used Combination Libraries, but they focus on other problems.
Many thanks for hints!
What you're looking for is a particular data structure called a Tree, but probably more specifically in your case, a Trie.
Trie data structures are commonly used in things like Autocomplete. With the image below, if someone typed "te", I can traverse the Trie and see what options would come after that (tea, ted, ten).
It looks like this would also fit your use case from what I can tell.

Linear Search works with written word by user? [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 5 years ago.
Improve this question
So i recently started learning C# and I have an assignment from my teacher.
I have been stuck on the last part of the code and would like to know from people with more knowledge what I am doing wrong or if there is something I have forgotten.
The assignment is to let the user write maximum 5 words. Then the user can view the word he/she wrote. This part I have done and it works.
However the search part is confusing me. Im using array and for-loop and the search part is still not working. Also worth mention is that I have assigned the array to Console.Readline();, meaning test[0] Console.Readline() and so on;, if it is of any help.
So in short, I want to have a linear search that can find the written word. Also whatever I type when doing the search it says that the word exists.
This is the part I am stuck.
If you have all the words stored away in an Array, just use Array.Contains like this
string[] userWords = { "word1", "word2", "word3", "word4" };
string search = Console.ReadLine();
if(userWords.Contains(search))
{
Console.WriteLine("Word " + search + " exists");
}
You have to include the System.Linq namespace for this to work.
If I have understood your question correctly, you want to find a string from an array of strings. You can do this using a for loop.
string search=Console.ReadLine();
for(int i=0;i<5;i++){
if(test[i]==search){
Console.WriteLine("word: " + search + " exists.");
}
}

C# Regex meet first match and take everything from start of string [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 5 years ago.
Improve this question
I have a string input which looks like:
var price = "£1.33(including postage)";
I'd like to take out the first part of the string before ( so that the output of regex would look like this:
"£1.33"
I'm new with Regex so I'm not quite sure how to do this, can someone help me out?
P.S. I thought of doing a substring, but that wouldn't work since price can have more decimals, and can be a larger price, so this option definitely wouldn't work
do you have to use regex?
much easier if you use split
string result = price.Split('(').First();
You don't need Regex for this if you have the same basic format of the "price" just different values.
var result = price.Substring(0, price.IndexOf("("));.

Generating folders using textboxes [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 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

C# Replace multiple spaces in a string with newline [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
This questioned has been asked before in regard to other languages but I could't find anything on using regex or any other algorithm to solve this in C#.
For example:
Photosynthesis maintains atmospheric oxygen levels and supplies all of
the organic compounds and most of the energy necessary for life on
Earth. Most cases, oxygen is also released as a waste product. (((((THIS SERIES OF SPACES HERE THAT SUGGEST THE END OF A PARAGRAPH))))
Although photosynthesis is performed differently by different
species, the process always begins when energy from light is absorbed
by proteins called reaction centers that contain green chlorophyll
pigments.
should be formatted as:
Photosynthesis maintains atmospheric oxygen levels and supplies all of
the organic compounds and most of the energy necessary for life on
Earth.
Although photosynthesis is performed differently by different species,
the process always begins when energy from light is absorbed by
proteins called reaction centers that contain green chlorophyll
pigments.
How do I get this done?
var SpacedText = "Some sample text. This should be a new paragraph."
var NewlineText = Regex.Replace(SpacedText , #"\s{2,}", Environment.NewLine);
Change the 2 in the regex for however many spaces you want it to break on.
Environment.NewLine can be replaced with whatever newline delimiter you need (<br /> for html, or any listed here).
The best guess that I can think of is to match the end of sentence . and possible trailing whitespace, before also end of line, and replace it with . and carriage return/linefeed.
In this case the regex would be
\.\s*[\r\n]+
http://regex101.com/r/cU2tF9/1

Categories

Resources