3

Hi I don't feel very well with regular expressions. What I would like to achieve is to extract a numeric substring (only 0-9 digits) from the input string.

  • The numeric string that is searched should be preceded only by a semicolon (;), space ( ) or should be placed exactly at the begining of the input (not line).
  • The numeric string that is searched should be followed only by a semicolon (;), the end of line or the end of the input string.

Exemplary input:

;x; ;SrvId=3993;ad257c823; 435223;

Output:

435223

I tried: [ \A|;|[ ]]\d*[\r|;|\Z] but it did not worked, it did not even compiled.

5 Answers 5

2

Try this one:

string resultString = null;
try {
    resultString = Regex.Match(subjectString, @"(?<=\A|\s+|;)(\d+)(?=$|;|\Z)").Groups[1].Value;
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

Break down :

(?<=\A|\s+|;)

Posiive lookbehind : start of input or at least one whitespace character or a semicolon.

(\d+) at least one digit

(?=$|;|\Z)

Positive lookahead either end of line, or semicolon or and of input.

Input : ;x; ;SrvId=3993;ad257c823; 435223;

Output of group 1 : 435223

Sign up to request clarification or add additional context in comments.

5 Comments

@HasanKhan Can you provide an input which makes it to match where it shouldn't match?
@FailedDev it doesn't match the number at the end of line when there is more text in new line
@HasanKhan I don't see any new line in OP examples. Do you?
@FailedDev "The numeric string that is searched should be followed only by a semicolon (;), the end of line or the end of the input string."
@HasanKhan You can write whatever bold letters you want, however this still doesn't change the fact that there is no new line in the OPs post :) Also if my english don't deceive me the end of line should be the last part of the string. OP doesn't mention possibility of multiple new lines. So you are wrong - again :D
1

Try this regex:

^(?:[; ]?)(?:.*?)([0-9]+);$

Comments

1

Using ^.*[ ;](\d+)[;\n]?$ will capture the numbers you're interested in although you may have to change the \n to \r\n depending on the line endings of your input file.

Comments

0

The regular expression should be this:

"[; ]{1}[0-9]+($|[^0-9]+)"

1 Comment

This returns 435223; which includes the semi-colon
0

Try using this expression

(\d+\.?\d*|\.\d+)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.