Check if textBox contains this specific character configuration - c#

I have a textbox with a long string of characters, and I need to check if a combination of 9 characters and numbers separated by hyphens exists (such as FSX-TVD-D5M). If that's found, copy only that bit of the string to another textBox.
I've looked at regex methods to accomplish this but haven't quite figured out a working method. I'd really appreciate some help.

/\w{3}-\w{3}-\w{3}/g
Please try like above.

Related

How to replace all linebreaks in the start of string c#

I've seen a lot of questions on how to replace all linebreaks.
However I have a scenario where I would like to replace all linebreaks in the start of the string only, there can be none, one or multiple and there is no way to know beforehand how many linebreaks it will be.
Question:
How do I remove all linebreaks that may occour before the string?
any help or input appreciated, thanks!
If you want to remove all leading occurrences of a set of characters, use String.TrimStart. Its prototype is:
public string TrimStart (params char[] trimChars);
In trimChars you put \n, in your case.

How to check if string has letter in second character with regex

Say I wanted to create an ID number such as 1A45 or 4F01.
What would the regex be to make sure that the string had exactly one letter as the second character?
I am unsure how to check for specific combinations of characters.
What I have so far is:
if(!Regex.IsMatch(txtTrainID.Text, #"^[\w,\d,\w,\w]+$"))
Which is obviously completely wrong, I've had trouble trying to find a decent simple answer to this anywhere.
If that's the only requirement (and I am sure it's not), use anchors and a character class in the second position as in
^.[A-Za-z]
See a demo on regex101.com.
What you probably mean, comes down to:
^\d[a-zA-Z]\d{2}$
The latter means one digit, one of a-zA-Z, followed by two other digits and the end of the string. See another demo on the same site.

how to match multiple words in string using regex?

I am trying to match 3 words that can appear anywhere in the string:
Win
Enter
Now
All 3 words must exist in the string for it return as a match. But I am having issues for getting a match when all 3 words do exist.
Below is the regex I am using: http://regexr.com/39b83
^(?=.*?win)(?=.*?(enter))(?=.*?(now)).*
Regex is working when all three words are within the same line... when its spread out across the entire string on different lines, it is failing to match.
Any direction or help is appreciated.
Since you don't want to match words like center (with the word "enter"), I would use:
/(\benter\b)|(\bwin\b)|(\bnow\b)/
Link to Fiddler
I think C# would support (?s) DOTALL modifier. If yes then you could try the below regex,
(?i)(?s)win.*?enter.*?now
How about...
/(win|enter|now)/gi
It sounds like you want to match the lines on which these words appear, across up to three lines. That’s not really easy, but:
/^.*win.*(?:\s+.*)?enter.*(?:\s+.*)?now.*|^.*win.*(?:\s+.*)?now.*(?:\s+.*)?enter.*|^.*enter.*(?:\s+.*)?win.*(?:\s+.*)?now.*|^.*enter.*(?:\s+.*)?now.*(?:\s+.*)?win.*|^.*now.*(?:\s+.*)?win.*(?:\s+.*)?enter.*|^.*now.*(?:\s+.*)?enter.*(?:\s+.*)?win.*/igm
should do it.
It 's because the dot doesn't match the newline character. To change this, you have to ways. The first, use the s modifier (that allows the dot to match newlines):
(?s)^(?=.*\bwin\b)(?=.*\benter\b)(?=.*\bnow\b).*
But this feature isn't always available (for example in Javascript). The second way consists to replace the dot with [\s\S] (a character class that matches all the characters):
^(?=[\s\S]*\bwin\b)(?=[\s\S]*\benter\b)(?=[\s\S]*\bnow\b)[\s\S]+

Parse directories from a string

Firstly i have spent Three hours trying to solve this. Also please don't suggest not using regex. I appreciate other comments and can easily use other methods but i am practicing regex as much as possible.
I am using VB.Net
Example string:
"Hello world this is a string C:\Example\Test E:\AnotherExample"
Pattern:
"[A-Z]{1}:.+?[^ ]*"
Works fine. How ever what if the directory name contains a white space? I have tried to match all strings that start with 1 uppercase letter followed by a colon then any thing else. This needs to be matched up until a whitespace, 1 upper letter and a colon. But then match the same sequence again.
Hope i have made sense.
How about "[A-Z]{1}:((?![A-Z]{1}:).)*", which should stop before the next drive letter and colon?
That "?!" is a "negative lookaround" or "zero-width negative lookahead" which, according to Regular expression to match a line that doesn't contain a word? is the way to get around the lack of inverse matching in regexes.
Not to be too picky, but most filesystems disallow a small number of characters (like <>/\:?"), so a correct pattern for a file path would be more like [A-Z]:\\((?![A-Z]{1}:)[^<>/:?"])*.
The other important point that has been raised is how you expect to parse input like "hello path is c:\folder\file.extension this is not part of the path:P"? This is a problem you commonly run into when you start trying to parse without specifying the allowed range of inputs, or the grammar that a parser accepts. This particular problem seems pretty ad hoc and so I don't really expect you to come up with a grammar or to define how particular messages are encoded. But the next time you approach a parsing problem, see if you can first define what messages are allowed and what they mean (syntax and semantics). I think you'll find that once you've defined the structure of allowed messages, parsing can be almost trivial.

RegEx Expression Help

I need a regular expression that will not let someone continue to check out if the type in the word 'box' or 'Box' or 'BOX' followed by numbers. Both conditions have to be met for this to work, the word box and the numbers not one or the other. Does anyone know how to write this?
EDIT:
Hey guys, I've tried a few and the problem is that it's preventing regular addresses like 1234 S. Something Lane. All I want to prevent is if the user types in something like p.o. box 1234. We're figuring anything with the word 'box' and numbers after it should not be accepted.
thanks
Edited after the question was clarified:
new Regex(#"\bbox\s*\d+", RegexOptions.IgnoreCase);
That will match "box" as a word by itself, optionally followed by whitespace, followed by a number, anywhere in the input string. So it will match:
box12
Box 12
P.O. Box 12
BOX 12a
It won't match:
boxtree 12
boombox 12
Box Pog
Box Pog 12
(It would have been very helpful if your original question had explained what you were actually trying to do!)
(box|Box|BOX)[0-9]+
EDIT after clarification of question:
I guess you actually want a case-insensitive search on:
box\s*\d+
\s*: Any amount of whitespace
\d+: At least one number
You can define the number of digits, if you want to. Example for 3-5 digits:
box\s*\d{3,5}
At least in my experience it's easier to write a RegEx to match the expected case strictly, rather than trying to exclude an exceptional case.
If you show an example of the format you're wanting to accept it will be easier for people to answer this.
If you're wanting to prefix a string with any variant of "Box" and then with numbers, something like this should work (make sure you use RegexOptions.IgnoreCase)
^box\d+$
Which would match for ...
box1
BOX1234567
bOx3
...etc...
have you tried something like
/box\d+/
plus IgnoreCase?
Don't forget the "post office" option.
^(p(ost|.) *o(ffice|.) )?box *[0-9]+$
this :
box|Box|BOX\d+
will match these specific cases box,Box,BOX and all these followed by numbers but it will also return true in this case for instance :
asBox123asdasd
If you want to get true if the string is exactly box123 for instance you should use :
^box|Box|BOX\d+$
instead

Categories