How to get the value behind or before "/" based on condition?

Hi,

I have a line “2016/2017 Semester 2”

If the line contains “Semester 2”, I will take the value behind “/”, which is 2017
If the line contains “Semester 1”, I will take the value before “/”, which is 2016

Do I have to use regular expression?

Thank you

Hi,

Please try this Regex : (?<=/)\d{4}

http://regexstorm.net/tester

hi @Ying_Zhen_Lee
try this,
Main.xaml (6.3 KB)

Hi @anil5,

Thanks for your reply!

However this regex only get the value behind “/”
What if I want to get the value before “/” ?

Thank you

Use this then

\d{4}(?=/)

Hi @samir,

Thanks for your reply, it works!

However may I ask what does this line mean ?

line

Thank you

Hi,

If semester 2 is constant and always and you want the values behind “” use this regex : \d{4}(?= Semester 2)

if Semester 1 is constant always and you want the values before “/” use this regex : \d{4}(?=/(\d{4})\s(Semester 1))

1 Like

@Ying_Zhen_Lee
basically it splits the string using “/” and " "(one Blank space)
like this,
2016 “/” 2017 " " Semester " " 2

so here,
“line.Split({”/"," “},StringSplitOptions.None)(1)
(1) is index of the array, to get 2017

“2016” —> (0)
“2017” —> (1)
" Semester" —> (2)
“2” —> (3)

if we didnt put (1) next to the split, it’ll split the string and convert it into array of String (String).
[“2016”,“2017”," Semester",“2”]

1 Like

Hi @samir,

Thank you for your explanation! :slight_smile:

Thank you

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.