0

How can I search a whole string for a specific match. It'll contain both characters with int or decimal numbers eg A12B32.25C-456D-75.E75 I'll know that this will start with A and ends with E I think I can use "^" and "$" right? but i'm bit lost in other parts to check for character and int or decimal. I'll be glad if you can give the regex and explain it a bit :).

PS. D-75. is not mistyped...

Thanks in advance.

2
  • 1
    You could give us some more info first. Perhaps some better example of those strings you want to match. Commented May 31, 2010 at 8:35
  • I'll be using this regex in C#. and for examples I know that whole string will starts with character A and a number, B a number, C and a number, D and a number and finally E and a number So again some example lines would be A12.2B-45.5C45.D-45E452 A-45.B5.455C-45.D447.1235E985 so i have to match all 5 characters with numbers, may b int or decimals. And point the C45. in first example and A-45. in 2nd example.. Commented May 31, 2010 at 8:47

2 Answers 2

2

As a free-spacing regex, hoping I'm guessing correctly what you meant:

^                   # start of line
A(-?\d+\.?\d*)      # match "A", followed by a number with optional sign and decimal part
B(-?\d+\.?\d*)      # same with "B"
C(-?\d+\.?\d*)      # etc.
D(-?\d+\.?\d*)
E(-?\d+\.?\d*)
$                   # end of line

This will capture the numbers into backreferences 1-5. If you tell us how you're planning to use the regex, I can refine my answer.

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

2 Comments

Thanks mate this works like a charm so "?" is for optional and "+" is for one or more and "*" is for zero or more. is it? sorry i'm not familiar with regex...
You're welcome. And you got the explanation for the regex quantifiers exactly right :)
0

You are doing literal pattern matching? - you have noticed that you embedded regex metacharacters like a period == . in your pattern.

Depending on what regex engine you are using there may be a 'pattern-only' option. For example

grep -F 'mypattern'  myfile

prevents the regex engine from seeing metacharacters in a pattern.

What environment/regex are you using?

1 Comment

I'll be using this regex in C#. and for examples I know that whole string will starts with character A and a number, B a number, C and a number, D and a number and finally E and a number So again some example lines would be A12.2B-45.5C45.D-45E452 A-45.B5.455C-45.D447.1235E985 so i have to match all 5 characters with numbers, may b int or decimals. And point the C45. in first example and A-45. in 2nd example..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.