Regex-code-for-getting-only-numbers-and-alphabets-between-2-words

Hi all,

From this text i want to get the values after : and before Flight no:

PNR: R96BMV Flight no:

Like "R96BMV

Now i am using (?<=PNR:).*?(?=Flight) this code but sometimes there is chances that spaces or tabs can come between the word PNR and Full colon “:”

Hi @jishnupnair1996,

Kindly try with this method, this might help you resolving this,

1.assign this text to a string variable or a just variable
2. then you can use Split method to the get the value (R96BMV), even if there is a chance of space or tab that can come between the word PNR and Full colon as you say.

split method like this,

  1. assiging the text to a variable,

myText = “PNR: R96BMV Flight no:”

  1. split method

mytext = mytext.ToString.Split({":"}, stringSplitOptions.RemoveEmptyEntries)(1).ToString.Split({" "},stringSplitOptions.RemoveEmptyEntries)(0).ToString

  1. you will get the value you want…!
1 Like

Hi @jishnupnair1996,

Please use the regex (?<=PNR:)[\s\S]*?(?=Flight) to capture everything in between ‘PNR:’ and ‘Flight.’

Warm regards,
Nimin

1 Like

Hi @nimin
i have tried it but if there is any spaces or tabs “\t” between word PNR and full colon “:” it will will not pick the value

hi @jishnupnair1996

Is that split method working, i checked from side, its picking…!

Even i m eagerly waiting for regexp way of approach…

cheers…!

Hi @Palaniyappan

Split string method is not preferring i am also using it now. but regex will be more stable

Hi @jishnupnair1996,

This regex will work, if there is a whitespace or “\t” in between ‘PNR’ and ‘:’
(?<=PNR[\s\S]:)[\s\S]*?(?=Flight)

Wam regards,
Nimin

Sorry @nimin

Now it is not picking the values for which there is no space between PNR and Full colon

@jishnupnair1996

Try this : (?<=PNR)\s*:(.*)(?=Flight)

1 Like

Hi @jishnupnair1996,

Please try this.
System.Text.RegularExpressions.Regex.Match("PNR : R96BMV Flight no:","(?<=PNR[\s]*:)(.*)(?=Flight)").ToString

Warm regards,
Nimin

1 Like

You also can use the split method.

[Name your string variable].ToString.Split({"-"},System.StringSplitOptions.None)

@jishnupnair1996

if you getting the string in this format

Why you are not just replacing “PNR:” and “Flight no:” from the string.

Kindly try with this @jishnupnair1996 ,

inside pattern in matches activity with input as your string variable,

  1. if you are clear that string between PNR: and FLIGHT NO:, is of this format like starting with
    string, with numbers in middle and ending with a string, like “R96BMV”, then use this

pattern = “\w{1,9}\d{1,9}\w{1,9}” \this searches for string with a character in the beginning with a number
in the middle and again a character in the end

It works…!

  1. if you are not clear with what the format would be of between PNR: and FLIGHT NO:, then use this

pattern = “[^\s][^\W]+” \this neglects the space, non-characters and gives out only the strings

…here the output will be of IENUMERABLE, like {“PNR”,“RB98BMV”,“FLIGHT”,“NO”}, you can get the
value you want, like as you said the second one.

Hi @Rashmi

it doesn’t giving a full match.

(?<=PNR{[\s]|:)(.*?)(?=Flight) but it gives a full match

1 Like

It’s giving the full match, Regex

yeah but it contains the colon also isnt
we dont need that colon

1 Like

Does that help you?

image

[a-zA-Z][0-9][0-9][a-zA-Z]*

it will give the value between PNR and Flight irrespective of “:”

image

1 Like

Try this,
“(?<=PNR:)\s?(\w*)\s?(?!=Flight no:)”