tags:

views:

69

answers:

2

Hi, 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  A: 

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.

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

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?

jim mcnamara
I'll be using this regex in C#.and for examplesI 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 numberSo again some example lines would beA12.2B-45.5C45.D-45E452A-45.B5.455C-45.D447.1235E985so 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..
sYl3r