C# Regex questions [closed] - c#

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

Related

How i can get the first match in regex and import this match in a 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 6 years ago.
Improve this question
For example using a regex expression del(.*?)del get delcatdel
and save the result in string or in text
word1word2delcatdelword3word4deldogdelword5
in my text (or string) i need obtain delcatdel
USING c sharp
please anybody help me
var regex = new Regex("del(.*?)del");
var match = regex.Match("word1word2delcatdelword3word4deldogdelword5");
string matched = match.Value;

Exclude match in Regular Expression c# [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
I would like to exclude a specific string when it is contained in an expression:
Example:
myurl.htm = exclude
myurl = include
I tried this one : ([a-z0-9]+)(?!.htm)
But looks like it doesn't work.
Try the following:
([a-z0-9]+)(?!^\.htm)
You had two errors in your expression:
You have to escape the dot . with a backslash because it means "matches any character (except newline)" unescaped.
You have to add a ^ to prevent cutting of the last character.
You can test your expression on this website: https://regex101.com/

How do I add text into my string? [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
I though it was simple. but I didn't manage to make this line of code add "\Game_Progress.xml" to my String
code_file.Insert(code_file.Length, "\\Game_Progress.xml");
You can append the string using the += operator:
code_file += "\\Game_Progress.xml";
Note: If you want to combine a path, you should consider using the Path Combine method:
System.IO.Path.Combine(code_file, "\\Game_Progress.xml")
C# can require an #prior to the string when using backslashes. Try:
code_file.Insert(code_file.Length, #"\\Game_Progress.xml");

Custom regular expression for string with number [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 6 years ago.
Improve this question
I have to validate a sting with regular expression.
String Should be in the NHCC-XXXXX-00 Format ,where X is a Number.
Correct strings:
NHCC-10010-00,
NHCC-78965-00,
NHCC-99654-00
Wrong strings:
NHCC-1001-00
NHCC-78965-0
NHC-99654-00
ASDF-99654-00
NHCC-F9654-00
NHCC-99654-01
Can any one help me to solve the above senario?
This should work:
"NHCC-\d{5}-00"
Demo
You need to use anchors inorder to do an exact match.
#"^NHCC-\d{5}-00$"
The regular expression you want is #"(?s)^NHCC-\d{5}-00$"
if (!Regex.IsMatch(input, #"(?s)^NHCC-\d{5}-00$"))
{
//not valid
}

Extract sentence containing a word from text [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 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.

Categories