I have this kind of string and I can’t get it to split how i want it.
“ID: 12345 Name: Firstname Lastname Position: Assistant”
I tried splitting it with Employee.Split(":"c) but than I get:
Firstname Lastname Position
Instead of this:
Is there a way to split a String at two positions at once?
for Example, to get the ID, i would need to split at “ID: " and " Name: *”
Thanks alot, Mahrie
Fine in that case we can use REGEX method instead of split method
For I’d value mention like this in assign activity
str_Id = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=Name:).*(?=Position:)”).ToString.Trim
str_Name = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=NAME:).*(?=NAME:)”).ToString.Trim
str_position = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=Position:).*”).ToString.Trim
If you have multiple values then use MATCHES instead of MATCH
Can you explain what all that in the brackets means?
What does it do? For example “?<”
It’s a typo from my side
Actually it is ?<=
It means if we know the position of one word or two words then we can get the word in between them
To denote in between them we use ?<= and ?=
Another solution :
If there are multiple records in your string, the following might be better.
mc = System.Text.RegularExpressions.Regex.Matches(yourString,"ID:\s*(?<ID>\w+)\s+Name:\s*(?<Name>\S+\s+\S+)\s+Position:\s*(?<Position>.*)")
Sample20211104-6.zip (2.5 KB)
you can easily remove both “ID” and “Position” label. So you can firstly split a string as you’ve mentioned and then remove “ID” and “Position” string from the list that you’ve created. List.Remove() method should get the work done.
If i do that, the variable just stays empty. Could it be there is another typo, not just the ?<= that you fixed on your post?
Thats seems really complex. Could you explain the what the code does?
I will try this in case the RegEx Version wont work, because now I kind of want to make it work.
Not sure if REGEX is really necessary in that case. If you’d like to stick with it, there’s a simpler method to use it - “Matches” activity. It does the same as the code showed by colleagues.
First, \s* or \s+ means 0 (or 1) or more whitespace. For now, replace it to " " to understand easily.
"ID: (?<ID>\w+) Name: (?<Name>\S+ \S+) Position: (?<Position>.*)"
(?<xxx>expression) means named group. We can get it by name as m.Group(“ID”).
To understand easier, use simple group.
"ID: (\w+) Name: (\S+ \S+) Position: (.*)"
\w+ means 1 or more word characters.
\S+ means 1 or more non-whitespaces.
So the above expression matches the following.
“ID: (1 or more word characters) Name: (1 or more non-whitespaces 1 or more non-whitespaces) Position: (0 or more any characters)”
Finally we can get characters inside parentheses by name like m.Group(“ID”).Value
I kept this in mind and tried it. Wasn’t exactly the solution, but I just used .replace() instead of remove, which than worked out for my case.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.