mahrie
November 4, 2021, 1:39pm
1
Hello everyone!
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:
ID
12345 Name
Firstname Lastname Position
Assistant
Instead of this:
12345
Firstname Lastname
Assistant
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
Hi
Fine in that case we can use REGEX method instead of split method
Like this
For I’d value mention like this in assign activity
str_Id = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=Name:).*(?=Position:)”).ToString.Trim
For Name
str_Name = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=NAME:).*(?=NAME:)”).ToString.Trim
For Postion
str_position = System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<=Position:).*”).ToString.Trim
If you have multiple values then use MATCHES instead of MATCH
Cheers @mahrie
mahrie
November 4, 2021, 1:49pm
3
Can you explain what all that in the brackets means?
System.Text.RegularExpressions.Regex.Match(Strinput.ToString,”(?<Name:).*(?=Position:)” ).ToString.Trim
What does it do? For example “?<”
Mahrie
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 ?=
Cheers @mahrie
Yoichi
(Yoichi)
November 4, 2021, 2:15pm
5
Hi,
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)
Regards,
Hello,
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.
Best,
Artur
mahrie
November 4, 2021, 2:26pm
7
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?
mahrie
November 4, 2021, 2:31pm
8
Thats seems really complex. Could you explain the what the code does?
mahrie
November 4, 2021, 2:32pm
9
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.
Best,
Artur
Yoichi
(Yoichi)
November 4, 2021, 2:50pm
11
Hi,
"ID:\s*(?<ID>\w+)\s+Name:\s*(?<Name>\S+\s+\S+)\s+Position:\s*(?<Position>.*)"
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>.*)"
Next (?<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
Regards,
mahrie
November 5, 2021, 8:30am
13
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.
Thanks everyone,
Mahrie
system
(system)
Closed
November 8, 2021, 8:30am
14
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.