tags:

views:

91

answers:

4

i have exemple

Term:a=27 B=90 C=65

....and i want only value C and A, C first and A second

i have do

(C=(\d+)^|A=(\d+))

but no success

why

please

+1  A: 

Usually, you shouldn't care if about the order that the matching parameters get set as you can change them in the surrounding code. To do such a 'normal' match you need something like this:

a=(\d+)\s+B=\d+\s+C=(\d+)

Your expression is looking for the C= bit or the a= bit: it won't match both at the same time. Also, as has been pointed out above, the '^' won't help.

I don't know of any easy way to switch the matching around inside the regular expression engine. It could be possible with interesting use of a positive look-behind (looking for the a bit after the C bit is matched, but I don't know that the order of the matching variables is well defined in such a case) but it's certainly possible in the surrounding code. One compact (ugly and probably insecure) way of achieving this is to abuse the eval function in something like perl to re-write the names of the matching variables. This works for me:

eval(s/a=(\d+) B=\d+ C=(\d+)/\$1=$2 \$2=$1/);
print "$1 \n"; #gives 65
print "$2 \n"; #gives 27

To give a more concrete solution we need to know a lot more about the system surrounding your regular expression. I doubt any 'pure' regex will be portable, or will strictly be regular.

Andrew Walker
A: 

The regular expression to match what you need is:

A=(\d+) B=\d+ C=(\d+)

But then you need to collect the ouput like this (depends on the tool or programming language that you're using):

\2 \1

It is \2 followed by \1 because you want the value of C first and then the value of A. These numbers reflect the order in which the expressions in parenthesis are in the regex.

Note: I validated this expression with Notepad++'s find and replace tool.

Bruno Rothgiesser
i cant change the order , i get from database c first and an second!!!
farka
@farka, your question doesn't say anything about databases - I didn't really understand what you need. Please edit your question to be more specific so that people can help.
Bruno Rothgiesser
ok i get from database first row C=(\d+) and second row A=(\d+), and i want c and A values the same time from term:a=27 B=90 C=65
farka
+1  A: 

In your regex you use the ^ symbol, this indicated the beginning of the string, so "c=(\d+)^" will never make a match, is it not trying to match something that is before the beginning of the string?

As far as I know regex cannot behave the way you want (as in one string returning two values the latter before the former), it is rather easy to do with two single expressions though and then just use the latter(C) before the former(a) as in pseudocode

match_for_a = "a=(\d+)"
match_for_c = "C=(\d+)"
do_something( match_for_c)
do_something( match_for_a)

You already have (well almost) the appropriate regex for each

a=(\d+)

and

C=(\d+)

EDIT: based on your comment, and my reply, here is some pseudo code for a function returning a tuple.

tuple match_c_and_a(){
  match_for_a = regex_match("a=(\d+)")
  match_for_c = regex_match("C=(\d+)")
  return (match_for_c, match_for_a)
}

But this kind of thing is not pure regex and is programming language dependent.

EDIT AGAIN I'm sorry if I miss understand you farka, but I cannot see anything in any of the answers submitted that prevents you from doing what you want.

Can you not just do

for every item in the database
   get the match for C
   do something with it
   get the match for a
   do something with it

Regex is able to match anywhere in the string, so it does not matter what order you get the items C and a in.

suicideducky
i want two value of same time, first values c then a , two in one regular expression
farka
Is there any reason you cannot do it in two different stages?The regex ((a=(\d+))|(C=(\d+)) would match both but it would not return them both at the same time.what you are asking for is *impossible* with plain regex although it is possible to write a function (language dependant) that returns an array (list) of all the matches, it is also possible to write a function that returns a tuple of (C, a) but this is not do-able with plain regex alone.
suicideducky
my algorithm musst be quickly, hier ist only one data, but i get differt 1000 data from file und regular expresion from database
farka
So you have 1000 inputs and you must get a C and a from each?For every input you have a C and an a you must get?
suicideducky
A: 

name/value pairs appear in any order please help

farka