Regex to Restructure the Input String

String can be any one of the two

  1. Intention: Singapore – SRC Refinery , here output should be - Singapore
  2. Intention: Singapore , here output should be - Singapore
  3. Intention: SRC Refinery – Singapore , here output should be - SRC Refinery

No matter what the string is I want output as the first complete name after intention: and before –

Regex.Match(sampleString, “(?<=Intention:)(.+)(?=–)”).Value

2 Likes

System.Text.RegularExpressions.Regex.Match(Str, “(?<=Intention:).*”).value

Can you explain the syntax like whats the role of ?<= and other symbols

?<= —> Startswith String regex pattern
?= —> endswith String regex pattern

what is the role of .+ after Intention:)(

@Gaurav07
. Matches any character other than newline (or including newline with the /s flag)
+ Matches one or more

(?<=Intention:)\s*\w*\s*(\s|\w*)

" . " Matches any character other than newline
" + " Matches one or more

tx… so this picks up the value between intention: and -
Now if I want to pick value between intention :(in the beginning) and in the end there can be – or- or /

Here I know the string will start with intention : but it can end with either - or / or –

Please provide code for such cases

@Gaurav07

(?<=Intention:).*?(?=–|-|_)

tx…
Now I have a string
WS 50.81 - Overage 50%
from this I need to take a value present after WS(50.81) and its not fixed that there is a - after it, there can be anything else, so basically I need the decimal number present after WS.

Similarly I want the no 50 present after Overage…

1 Like

@Gaurav07

(?<=WS)[\d \.]+

(?<=Overage)[\d %]+

tx and this one also :slight_smile:

$35,000 – per day/pro rata - 120 hours

I need just 35000 from this string

@Gaurav07
\d.*(?=–)

its not necessary that there will be a - after 35000, only thing fixed is the $ sign, it can be like this

$35000 – per day/pro rata - 120 hours ( space between and 35000)

$35000 – per day/pro rata - 120 hours ( no space)

Hi @Gaurav07,

You can use this pattern to match the first complete name after intention:

Pattern = "(?<=Intention: )[A-Za-z ]+"

its not necessary that there will be a - after 35000, only thing fixed is the $ sign, it can be like this

$35000 – per day/pro rata - 120 hours ( space between and 35000)

$35000 – per day/pro rata - 120 hours ( no space)