Subject: regex
Posted By: mrame Post Date: 7/24/2008 11:41:14 AM
Hi,
I have a text as shown below. I need regular expression to match the conditions and replace. The number at two positions should be same(1 or 2 or any). The letter M, may be M|N|Y|O. check for two commas also

match: M1,1,
replace: ,

any help is appreciated

Reply By: mhkay Reply Date: 7/24/2008 12:08:59 PM
You haven't specified very clearly, but it looks as if the regular expression you want might be

[MNYO]([0-9]),\1,

Remember that when you are doing a replace, it's perfectly OK to have a regex that matches strings that will never arise, so you could just as well use for example

.([0-9]),\1,

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply By: mrame Reply Date: 7/24/2008 12:27:39 PM
error arises if i use the below statement for $check4 as <AID>M1,1,<AID>M2,2

<xsl:value-of select='replace($check4, "[MNYO]([0-9]),\1,", "")'/>

The numbers will be same at both the positions, i.e, M1,1 and M2,2

Reply By: mhkay Reply Date: 7/24/2008 12:34:46 PM
Please try to be a bit more considerate. Doesn't it occur to you that I'm more likely to be able to explain the error to you if you tell me what the error message is?

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply By: mrame Reply Date: 7/24/2008 12:40:23 PM
what is meant is its not giving the needed output.

Reply By: mhkay Reply Date: 7/24/2008 12:51:50 PM
Then tell me what output it gives...

(Gosh, this is hard work)

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply By: mrame Reply Date: 7/24/2008 1:06:20 PM
it does not replace anything at all. the replaced text is as such $check4

Reply By: mhkay Reply Date: 7/24/2008 1:38:56 PM
You seem to want to do two things, to check whether the value matches "[MNYO]([0-9]),\1,", and if it does, to replace the commas.

So I would suggest

if (matches($check4, '[MNYO]([0-9]),\1,')
then replace($check4, ',', '')
else error()

(or perhaps "else $check4", I don't know)

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply By: mrame Reply Date: 7/24/2008 1:50:42 PM
Mike, $check4 will contain M1,N1,. So only i want to replace it.
Let $check4 be &lt;AID&gt;M1,1,&lt;AID&gt;M2,2

The output should be ,
I want to replace M1,1, with just ,
Also 1 will the same at both the places. It will change in he next occurrence as 2 (see $check4 above).

In ($check4, '[MNYO]([0-9]),\1,'),  ([0-9]) will check for any number it seems. But I want to check for only 1 at both the positions. At the next occurrence the search will be for 2. I should go one by one only. If I use ([0-9]),\1, the find will go to M2,M1. So only I'm checking for M1,1, especially. I think this might be clear to you now.


Reply By: mrame Reply Date: 7/24/2008 2:08:54 PM
ok lets try for this condition.

INPUT: M1,N1,
REPLACE: ,

1 will be same at both the positions. It will change as 2 at the next occurrence.

Reply By: mhkay Reply Date: 7/24/2008 2:16:19 PM
No, I'm sorry, you're not explaining the requirement clearly enough for me to be able to help with it.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply By: mrame Reply Date: 7/24/2008 2:19:40 PM
Let it very clear.

INPUT: M1,N1,
REPLACE: ,

1 will be same at both the positions

Therefore ([0-9]) in [MNYO]([0-9]),\1 should look to 1 only. This would be clear.

Reply By: mrame Reply Date: 7/25/2008 9:37:15 AM
Mike, I really appreciate your patience in replying for all my questions. Thank you.


Go to topic 73005

Return to index page 1