tags:

views:

107

answers:

5

I have this string

"ABC-2341241244 | tb1 | value | tb2 | value | tb10 | value"

How can I do with regex to replace for example '| tb2 | value ' with '' to stay with this,

"ABC-2341241244 | tb1 | value | tb10 | value"

I know the value 'tbxx' is a varieble that I have.

The Regex engine is javascript sorry not .Net C# sorry

+2  A: 

I think we need to know more about the patterns that you're trying to replace to answer.

Why aren't you using something like split in your language? (E.g., split on | to get an array.)

Benjamin Oakes
IMO this shouldn't be an answer, it should be a comment on the question, since it's essentially a request for more information.
Alison R.
I was thinking that way, but seeing the example above, i think that is the best way but i'm having trouble to do that... help anyone?
SlimBoy
+3  A: 

The exact syntax depends on the language you are using, but you need to escape the | character since it has special meaning inside regular expressions.

In perl you would do something like:

my $string = "ABC-2341241244 | tb1 | value | tb2 | value | tb10 | value";
$string =~ s/\| tb2 \| value //;

In java it would be:

String string = "ABC-2341241244 | tb1 | value | tb2 | value | tb10 | value";
string = string.replaceAll("\\| tb2 \\| value ", "");
sea36
ok... but my problem is that value is not "value", can have letters and numbers
SlimBoy
A: 

javascript replace

var 
    deletePair = 'tb2',
    strIn = "ABC-2341241244 | tb1 | value | tb2 | toDeletevalue | tb10 | value",
    pattern = new RegExp("\\| " + deletePair + " \\| ([a-zA-Z0-9]+) ", "g"),
    result = strIn.replace(pattern, '');

document.write(result === "ABC-2341241244 | tb1 | value | tb10 | value" 
    ? 'ok'
    : 'failed'
);
document.write('<br />' + result);

edit

Added deletePair in which you specify what key in your key | val representation you want to delete

Juraj Blahunka
ok... but my problem is that value is not "value", can have letters and numbers
SlimBoy
A: 

You don't have enough information for a generic solution[1]. Is your string guaranteed to be regular?

It sounds like your data is well formed - you should parse it in some way:

Python:

import csv
input = "ABC-2341241244 | tb1 | value | tb2 | value | tb10 | value"
parts = input.split("|")
r = csv.reader([input],delimiter='|')
parts = r.next()
try:
  i = parts.index(" tb2 ")
  parts = parts[0:i] + parts[i+2:]
except ValueError:
  pass

print "|".join(parts)

prints:

ABC-2341241244 | tb1 | value | tb10 | value

[1] Is your delimiter '|'? Or is it ' | '? Can you escape | in your data? Are you only searching on tbXX identifiers?

MikeyB
my delimiter is ' | '. i want to do what u just did
SlimBoy
How do i do that in javascript?
SlimBoy
A: 

If you regexp engine allows the following kind of substitution, something along these lines might work:

s/^\([^|]*|[^|]*|\)[^|]*|[^|]*|/\1/

That is, the beginning of the line up to the fields to be removed is replaced with the beginning of the line, i.e. the initial fields within \( \), which is referred to by \1. This works in sed and similar, and usually the same functionality is available, possibly with a different syntax.

Edit: Above I misunderstood that you wanted to remove two fields based on their position on the line, but I guess you wanted to remove “tb2” and the field after it, so:

s/^\(.* | \)tb2 | [^|]*|/\1/
Arkku