开发者

Regular expression problem

i have exemple

Term:a=27 B=90 C=65

....and i want o开发者_StackOverflownly value C and A, C first and A second

i have do

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

but no success

why

please


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜