String replace and regexp

F

Fabian Vilers

Hi all,

I'm looking for a regular expression that can do the following:

if matches: a~b§c~d
replace by: a~§c~

Sorry for posting something so stupid but I can't understand a word on
regexp :(

Thanks in advance,
 
E

Evertjan.

Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
Hi all,

I'm looking for a regular expression that can do the following:

if matches: a~b§c~d
replace by: a~§c~

Sorry for posting something so stupid but I can't understand a word on
regexp :(

If you do not understand, how would you possibly know it is stupid,
or that regex would be a good solution?

At least you would have to declare what you mean by ~ and §.

As I read it now, this code will do what you ask:

s = 'a~b§c~d'
alert(s)
s = 'a~§c~'
alert(s)
 
M

marss

if matches: a~b§c~d
replace by: a~§c~

If to take your sample as real data:

var s = "bla-bla~b§c~done";
var s1 = s.replace(/a~b§c~d/g, "a~§c~");

But if it is only sample I agree with Evertjan: you would have to
declare what you mean by ~ and §.
 
F

Fabian Vilers

marss said:
But if it is only sample I agree with Evertjan: you would have to
declare what you mean by ~ and §.

Thanks all.

I can give you more explanation here.

I've a string containing this:

var data_table =
'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC';

What I need is:

If the regexp matches a~b§c~d, where a, b, c, d can be any combination
of letters, replace it by a~§c~ or said differently, drop b and d.

Am I much clear with theses information?

Thanks again in advance for helping,
 
M

marss

'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC';
var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";
var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

What I need is:

If the regexp matches a~b§c~d, where a, b, c, d can be any combination
of letters, replace it by a~§c~ or said differently, drop b and d.

But it is not clearly stated what to do when we have a~b§c~d§e~f
pattern (your data string ends with
Magenta~RECUP§PaleGreen~CF§Khaki~ANC')
 
F

Fabian Vilers

marss said:
var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

Thanks! You're a regexp god ;-)
But it is not clearly stated what to do when we have a~b§c~d§e~f
pattern (your data string ends with
Magenta~RECUP§PaleGreen~CF§Khaki~ANC')

Yes, forgot to say :(

I can have 4 possibilities:
- a~b
- a~b§c~d
- a~b§c~d§e~f
- a~b§c~d§e~f§g~h

If it's one the last three, b, d, f and h must be removed.

How can I change the regexp?
 
E

Evertjan.

Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
Thanks! You're a regexp god ;-)


Yes, forgot to say :(

I can have 4 possibilities:
- a~b
- a~b§c~d
- a~b§c~d§e~f
- a~b§c~d§e~f§g~h

If it's one the last three, b, d, f and h must be removed.

How can I change the regexp?

This still is not right.

We should know what the first letter not in the string after b,d,f or h is

Or:

a~b§c~da~b§c~d

what is the delimiter between d and a?
 
F

Fabian Vilers

Evertjan. said:
a~b§c~da~b§c~d

what is the delimiter between d and a?

It depends. It could be either a '!' or a ';'.

In fact, a got a lot of information inside a string. My problem (see
other post) is that I worked with the string splited into arrays. But
looping in these arrays was throwing the warning message box about
infinite loops.
 
E

Evertjan.

Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
It depends. It could be either a '!' or a ';'.

Please always specify fully!
In fact, a got a lot of information inside a string. My problem (see
other post) is that I worked with the string splited into arrays. But
looping in these arrays was throwing the warning message box about
infinite loops.


<script type='text/javascript>

var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;"+
"Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";

s = s.replace(/([^~§!;]+~)[^~§!;]+([~§!;]||$)/g,'$1$2')

alert(s)

</script>
 
E

Evertjan.

Evertjan. wrote on 06 feb 2006 in comp.lang.javascript:
Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
Evertjan. wrote:

<script type='text/javascript>

var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;"+
"Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";

s = s.replace(/([^~§!;]+~)[^~§!;]+([~§!;]||$)/g,'$1$2')

Even simpler, just delete anything between ~ and § or ! or ; or EOL:

s = s.replace(/~[^~§!;]+([§!;]||$)/g,'~$1')
 
M

marss

Fabian said:
I can have 4 possibilities:
- a~b
- a~b§c~d
- a~b§c~d§e~f
- a~b§c~d§e~f§g~h

If it's one the last three, b, d, f and h must be removed.

How can I change the regexp?

If I 've got it right pattern a~b should not be changed.

var s1 = s.replace(/(§\w+~)\w+([!;]|$)/g,
"$1$2").replace(/(\w+~)\w+(§)/g, "$1$2");

If the regexp matches a~b§c~d, where a, b, c, d can be any combination
of letters

If you assure that a, b, c, d contains only letters, number or
underscore use \w,
if it can contain other symbols use [^~§!;] insead of it (as Evertjan
suggested)
 
T

Thomas 'PointedEars' Lahn

marss said:
var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!Khaki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§Khaki~ANC";
var s1 = s.replace(/(\w*~)\w*(\§\w*~)\w*/g, "$1$2");

/x*/ matches the empty string, too, for any /x/. To fulfill the OP's
requirement --

-- where it remains to be discussed what characters are regarded as letters,
\w matches only _ASCII_ letters --

-- use the `+' quantifier instead of the `*' quantifier.

And please learn to quote.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>


PointedEars
 
T

Thomas 'PointedEars' Lahn

marss said:
Why is the wrong answer cited when the corrections are already made?

Probably because USENET is not a real-time communication medium :)


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 6 Feb 2006 09:52:07 remote, seen in
news:comp.lang.javascript said:
I'm looking for a regular expression that can do the following:

if matches: a~b§c~d
replace by: a~§c~

Sorry for posting something so stupid but I can't understand a word on
regexp :(


An example is rarely sufficient to define a task. Describe it in words,
and then give examples to illustrate those words. You can also post in
both English and French, which may well help understanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top