By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

String replace and regexp

P: n/a
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,
Feb 6 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 6 '06 #2

P: n/a
>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 §.

Feb 6 '06 #3

P: n/a
marss wrote:
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!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~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,
Feb 6 '06 #4

P: n/a
> 'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~CF§Khaki~ANC';

var s =
"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~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')

Feb 6 '06 #5

P: n/a
marss wrote:
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?
Feb 6 '06 #6

P: n/a
Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
marss wrote:
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?


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?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 6 '06 #7

P: n/a
Evertjan. wrote:
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.
Feb 6 '06 #8

P: n/a
Fabian Vilers wrote on 06 feb 2006 in comp.lang.javascript:
Evertjan. wrote:
a~b§c~da~b§c~d

what is the delimiter between d and a?

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!K haki~ANC;"+
"Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleGreen~CF§ Khaki~ANC";

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

alert(s)

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 6 '06 #9

P: n/a
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!K haki~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')

alert(s)

</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 6 '06 #10

P: n/a
Fabian Vilers wrote:
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)

Feb 6 '06 #11

P: n/a
marss wrote:
'Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~CF§Khaki~ANC';
var s =

"Magenta~RECUP;Khaki~ANC;Magenta~RECUP§Khaki~ANC!K haki~ANC;Magenta~RECUP§Red~MAL;Magenta~RECUP§PaleG reen~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 --
If the regexp matches a~b§c~d, where a, b, c, d can be any combination
of letters,
-- where it remains to be discussed what characters are regarded as letters,
\w matches only _ASCII_ letters --
replace it by a~§c~ or said differently, drop b and d.


-- 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
Feb 6 '06 #12

P: n/a

Thomas 'PointedEars' Lahn wrote:
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 --


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

And please learn to quote.


I am learning.

Feb 6 '06 #13

P: n/a
marss wrote:
Thomas 'PointedEars' Lahn wrote:
> 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 --


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


Probably because USENET is not a real-time communication medium :)
PointedEars
Feb 6 '06 #14

P: n/a
JRS: In article <43*********************@reader0.news.be.easynet.n et>,
dated Mon, 6 Feb 2006 09:52:07 remote, seen in
news:comp.lang.javascript, Fabian Vilers <fv*****@be.keyware.com> posted
:
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 6 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.