how can i count multiple delimiters from a sentence and split it too?
ex:
apple … iPhone … MacBook.
so the result should be 2
if apple … iPhone
then result should be 1
Please help here how it can be done.
how can i count multiple delimiters from a sentence and split it too?
ex:
apple … iPhone … MacBook.
so the result should be 2
if apple … iPhone
then result should be 1
Please help here how it can be done.
Try this below expression
System.Text.RegularExpressions.Regex.Matches(str,"(?<=[a-z]\s)...").Count
Regards
Gokul
Hi @Sweety_Nagar,
Can you just clarify whether you need the apple, IPhone from the sentence or the special characters present in the string?
Regards,
@90s_Developer
i am not getting proper result
i take the same regex and put my string and result should be 2 but it is showing 7.
the above count is showing 7 as result but in below screenshot you can see we have 2 times “…”.
below is the regex as you suggested.
Can you share the Input here. I will check in my die also
Regards
Gokul
i want to count the strings or we can say we want to split some sentences with the delimiter " … " So as i described in above screenshots i want to count how many times " … " occurs and then on that basis want to split as " … " this can occur sometimes twice or sometime one time only.
“i am sweety nagar learning Uipath. … Software Engineer at xyz company. Pune … learning uipath from uipath forum.”
result should be 2.
Fine
Let’s take a simple method
Use this in a writeline activity like this
Split(yourstringvariable.ToString, “…”).ToArray().Count.ToString
Cheers @Sweety_Nagar
Hi @Sweety_Nagar ,
Here is a very simple logic that ought to do the trick:
Regex.Matches(str_rawString,"\.\.\.").Count()
Ensure you have imported System.Text.RegularExpressions from the Import Tab below:
MatchThreePeriods.xaml (4.8 KB)
Kind Regards,
Ashwin A.K
Hi @Sweety_Nagar,
You can either use the suggested solution from @Palaniyappan with a slight edit subtracting -1
Split(TestString.tostring,"...").ToArray.Count-1
Why? The -1
here is important as the spilt function will return an array and your aim is to count the number of seperators and not number of items in the array so we use subtract 1 from the count.
Or
Use solution from @ashwin.ashok with regex pattern matches, which checks your string and finds out the number of times the target character/pattern is found in the string. This approach does not require any other manipulations other than importing the RegularExpression namespace.
In short both the above solutions will return a value of 4
as the result to the following TestString
:
"i am sweety nagar learning Uipath. ... Software Engineer at xyz company. Pune ... learning uipath from uipath forum. ... Test1 ... Test2"
@Sweety_Nagar you can get the delimeter count using the below expression
System.Text.RegularExpressions.Regex.Matches(InputString, “[…]{3}”).Count
Also, you can directly get the string count without do any splittings
System.Text.RegularExpressions.Regex.Matches(InputString, “\w+”).Count
Please find the below workflow for both the scenarios
Splitstring.zip (2.1 KB)
use string.split("…"c).toarray()
and use array.length-1,
it will give you answer
regards,
veeraraj S
Hi @Veera_Raj ,
The approach you suggest here wont work because the expression will not validate. string.split("..."c).toarray()
will fail because ...
is not one character, it is three characters and when you use c
you have to explicitly use a character not multiple characters. Split method cannot split at multiple characters.
Hi @ushu
Both the suggestion will fail and can provide wrong results.
Use the following test string :
Your first expression needs to be edited and when edited with escape character it is no different from the one already suggested by @ashwin.ashok
Expression should have escape characters for .
character System.Text.RegularExpressions.Regex.Matches(InputString, “[\.\.\.]{3}”).Count
The second suggested expression System.Text.RegularExpressions.Regex.Matches(InputString, “\w+”).Count
will fail because \w+
will look for individual words. You have to specifically ensure that the pattern matches until the three dots ...
and in the end the output count should be subtracted with 1 to get the correct number of times the ...
separator occurred.
You can however use this expression and will not need any int manipulations
System.Text.RegularExpressions.Regex.Matches(InputString, “(.+?)\.\.\.|^(.+?)”).Count
@Sweety_Nagar a short summary of the above suggested approaches with your test string
CountSplits.xaml (9.7 KB)
Thanks @jeevith for your suggestions
I have tested with the input string you have provided
i am sweety nagar learning Uipath. … Software Engineer at xyz company. Pune … learning uipath from uipath forum.
I was able to get the right delimeter count with the expression
System.Text.RegularExpressions.Regex.Matches(InputString, “[…]{3}”).Count
This is FYI.
[]
matches any single character in character_group. This means [...]
equals [.]
. And we don’t need to escape dot character between [
and ]
in this case.
So, System.Text.RegularExpressions.Regex.Matches(InputString, “[...]{3}”).Count
will work well, but System.Text.RegularExpressions.Regex.Matches(InputString, "[.]{3}").Count
OR System.Text.RegularExpressions.Regex.Matches(InputString, "\.{3}").Count
is a simpler expression.
Regards,
My apologies for the confusion.
The reason why I said it wont work was because regex101 website fails to parse the [...]{3}
expression and returns nothing back. Regex101 is my default choice when checking for patterns. Now I know that some limitations exists in regex101. In short, REgex101 requires escape character to get the matches for this expression.
When I test the same expression in regextester, [...]{3}
is a valid expression and does not need escape characters as @Yoichi said.
@Sweety_Nagar UiPath also can parse the [...]{3}
expression correctly. Updated screenshot.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.