question

PARTHDESAI-2292 avatar image
0 Votes"
PARTHDESAI-2292 asked YitzhakKhabinsky-0887 commented

Regular expression

What should be the regular expression for
<something>_<something else>.xml

I am trying below but It doesn't return any matched for valid pattern.

Regex.Matches(file.Name, @"^.[a-z0-9][_][a-z0-9].[Xx][Mm][Ll]$")

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@PARTHDESAI-2292,

It is not clear what you are trying to achieve.
Are you tryng to get a particular part of the file name?

Please edit your original question, and add what your final goal is.

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

This way should work too:

 Regex.Matches(file.Name, "[a-z0-9]+_([a-z0-9]+[_-])*[a-z0-9]+[.]xml$", RegexOptions.IgnoreCase);

Since the suffix may be uppercase or lowercase, you need to use the Regex.Matches overload with RegexOptions as a parameter.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered PARTHDESAI-2292 commented

Theoretically, “something” can be expressed with “.+”, but it seems that you only accept letters and digits, therefore try this expression:

(?i)^[a-z0-9]+_[a-z0-9]+[.]xml$

If you need something special, then explain the rules and show examples.

(To insert the code try using the "101" button, because your expression is probably distorted).

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

Your expression is not working.
My input can be
Definition_53d60b07-4d5b-bc8f-e1f8-fe261f58caae.xml
Definition_53d60b07-4d5b-bc8f-e1f8-fe261f58caae.XML
File_Name_53d60b07-4d5b-bc8f-e1f8-fe261f58caae.xml
File123_53d60b07-4d5b-bc8f-e1f8-fe261f58caae.xml
File_123_53d60b07-4d5b-bc8f-e1f8-fe261f58caae.XML

In above case file extension can be in both lower case and upper case.
Before extension It will always have '_<Alpha numeric string having only '-' as special character in it>
Whereas It will always start with any string or characters excepted by file name.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

For your new details, try this expression:

 Regex.Matches(file.Name, @"(?i)^(?! )[^\x00-\x1F\\/.*?:<>""|]+_[a-z0-9-]+[.]xml$")

You can adjust it to allow the valid '.' inside the names.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.