Using regex to extract code from String

Hello again, and thank you for the attention!

Hopefully I can get an easy, straightforward solution in how to do this. I need to extract a certain code from a string that can be anywhere in said string, but before a “§” character.

example strings:
“Lorem Ipsum §001”
“§001 Lorem Ipsum”
“Lorem §001 Ipsum”

Regardless of where the “§001” is in the string, I want to extract only the code 001 and then assign it to a string variable. I have tried most examples and solutions given through this forum, but can’t find any way to do this. It should be easy in my mind, take anything following the “§” character, and stop when there is a space or end of string.

Thanks for your time!
Jakob

Hello,

Can you clarify after the character “§” do we get only the number? If it is the number then how many digits? Is it fixed or dynamic?

Clarify these

Thanks

Thank you for the swift reply! Yes, sorry for not being clear, but it is always a number, but it is dynamic in length. that being said, it could be “100032” or “0012”.

Thank you, ksrinu!

\d+ in regex will give you the numbers

but it will identify all the numbers, can you confirm that number will appear with the “§” and there are no other numbers?

Thanks

§(\d+) in regex will give the output :

§001
§001
§001

after that you can use replace and replace the §

Hope this helps

Thanks

Hello ksrinu!

The string may contain other numbers sadly…

No problem,

then you can use

(?<=§)(\d+)

It will identify the numbers which is with “§” character

Hope this helps

Thanks

Hi
Hope this expression would help you
str_output = System.Text.RegularExpressions.Regex.Match(str_input.ToString,”(?<=\W)\d+”).ToString

Cheers @Jakob_Petersen

I am getting this error, sadly:

Untitled

Thanks a bunch!

Kindly bound the regex expression with double quotes When passing them as argument
Cheers @Jakob_Petersen

Thank you for the correction, @Palaniyappan . However, I am shooting blanks with both expressions:

System.Text.RegularExpressions.Regex.Match(descString, “(?<=§)(\d+)”).ToString

System.Text.RegularExpressions.Regex.Match(descString.ToString, “(?<=\W)\d+”).ToString

Resulting in the same 3 empty strings:

I tested the expression in the wrong solution, so therefore the empty strings, it works perfectly, thanks!

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