I apologize if there is not enough to describe.
I'd like to search in the array using the regular expression to get the "most elemental string" in the "match string".
If you throw a string (=inData, variable) and a delimited character (=delimiter, variable, may use regular expressions),
I created a function (GetSplitData) to do the above.
The following is an example of the movement when there was only one part that matched the delimiter character.
This is working as expected.
"I am worried about the movement of ""there were multiple parts that matched the delimiter"" this time."
The desired shape is as follows:
As I use Regex.split, the current movement is
"AAA", "00", "BBB", "0000", and "CCC" are included in the String array.
I can't think of a way to get the "0000" at the back from here.
I was thinking of licking the array again with a delimiter if there are more than one, but
How to lick an array using a regular expression didn't come up when I searched.
Also, where the delimiter at the back was acquired,
I can't even think of a way to get it before and after it.
(I thought I could use LastIndexOf without regular expressions...)If I use it, will I go to search for the string "[0-9]+"?)
(Is it possible to use regular expressions in LastIndexOf just because I failed to find out?)
Please let me know if there are any methods, algorithms, or ideas that can be used.
I would like to ask for your advice.
I look forward to your kind cooperation.
·Microsoft Visual Studio 2013 Express for Windows
· .NET Framework 4.5.1
I apologize for the confusion.
The criteria for adopting delimiter is not string length, but the one at the one at the back.
If you receive an inData called "AAA0000BBB00CCC" and a delimiter called "[0-9]+",
"The ""00"" will be used for the delimiter."Because it's at the back.
I mistook x in array[x] for the number of elements.
I am very sorry that I confused you because I didn't use the term correctly.
Dear Sayuri and pgrho, thank you for your reply.
This time, Sayuri, who answered earlier, will be the best answer.
Pgrho's writing method using lambda style and linq was also very instructive.I will refer to it somewhere else.
Here's a program that actually worked.
I'm not sure if you want to search like
LastIndexOf or determine by the number of characters, but why don't you call
Regex.Matches and use
Match for the longest or last time?
If you match a regular expression to more than one place, whether it matches a particular place or not itself affects subsequent results.Therefore, you should avoid rewriting the regular expression itself.
If you match the string
aaba with the pattern
(a+|aba), the possible locations are
Yes, but if you match it with
Regex.Matches, you get
aa (2.) and
This is because
aa excludes 1. and 3. and 4. that begin halfway through
However, if you change the pattern to
(a+|aba)(?!.*(a+|aba)), 1. and 2. do not match because of the fourth character
a, so 3. and 4. begin with the second character and match longer
Of course, you can re-open
aba both end positions are the same, but it's hard to explain the specification why
aba is the delimiter instead of
a to people who don't know how to implement the
Regex.Matches follows the basic rules of regular expression, so we recommend this for ease of understanding to third parties.
If you use negative look-ahead, there were cases where it would be easier to understand, so I will only list the results.The cause is as explained above.
"a", "b", "c"
"a", "", "c"
© 2022 OneMinuteCode. All rights reserved.