How can i split this string in XSLT
cast one : 'T','0','1','2'
cast two: 'T','0','1'
case three: 'T','0'
i tried substring but the result is wrong
substring-before(substring-after(String,','),',')
i want the result to be 0 after split
For XSLT 1.0, you could apply additional substring-before() and substring-after() for the ' character:
substring-before(substring-after(substring-after("'T','0'", ','), "'"), "'")
For XSLT 2.0 (and greater), there are additional options.
You could use the tokenize() function to split the string by ,, and then select the second item, and then use translate() to translate the ' character into nothing:
translate(tokenize("'T','0','1','2'", ',')[2], "'", "")
or instead of translate(), you could use replace() with a regex capture group and then select the value inside of the single quotes:
replace(tokenize("'T','0','1','2'", ',')[2], "'(.*)'", "$1")
Related
For example the following code:
$test_str = 'foo(bar';
#arr = split('(', $test_str);
causes the 500 error
Why?
As ikegami says, split expects a pattern as its first argument. A string will just be converted into a pattern. Because an open parenthesis ( has a special meaning, this will error. You need to escape it.
my #arr = split /\(/, $str;
According to perldoc -f split, the first argument to the split() function is a regular expression /PATTERN/. So if you were to write this:
split('some text', $string)
it would be equivalent to:
split( m/some text/, $string )
And if some text contains characters that are special in regular expressions, then they will treated as such. So your line:
#arr = split('(', $test_str);
will be treated as:
#arr = split( m/(/, $test_str );
This is likely not what you want, as m/(/ is an invalid (you could say incomplete) regular expression. To match on a literal (, you need to escape it with a back-slash, so use this instead:
#arr = split( m/\(/, $test_str );
By now you've noticed that Perl tries to be helpful by converting your first argument from the string '(' to the regular expression pattern m/(/. Although you can pass in a string as the first argument, I don't recommend it -- use m/PATTERN/ instead.
The reason for my recommendation is because:
A pattern makes it clear that the first argument is a regular expression pattern, and not just any old string.
According to perldoc -f split, there is a special case where you can pass in a string as the first argument:
As a special case, specifying a PATTERN of space (' ') will
split on white space just as "split" with no arguments does.
Thus, "split(' ')" can be used to emulate awk's default
behavior, whereas "split(/ /)" will give you as many null
initial fields as there are leading spaces. A "split" on
"/\s+/" is like a "split(' ')" except that any leading
whitespace produces a null first field. A "split" with no
arguments really does a "split(' ', $_)" internally.
It's good not to confuse the two. So use ' ' as the first argument to split() when you want to use the special case of splitting on whitespace, and use m/PATTERN/ as the first argument for every other case.
I have a table with a column of string. within the string there are single quote which I want to get rid of all single quotes.for example:
"''hey, hey, we're the monkees''"
my regex works perfect and select all the values containing single quotes.
select regexp_replace(colName, '%''%', '') from tblName;
but it does not update my table when I want to replace this regex with nothing.
UPDATE tblName SET colName = regexp_replace(colName, '%''%', '');
I also checked this one
UPDATE tblName SET colName = replace(colName, '%''%', '');
Different functions and operators in Postgres use one of three different pattern matching languages, as described in a dedicated section of the manual.
The % form you are using here is the SQL LIKE syntax, where % represents "any number of any character". But the function you are using, regexp_replace, expects a Posix regular expression, where the equivalent would be .* (. meaning any character, * meaning repeat zero or more times).
Also note that LIKE expressions have to match the whole string, but a Posix regex doesn't, unless you explicitly match the start of the string with ^ and the end with $.
So the direct translation of '%''%' would be '^.*''.*$', giving you this:
UPDATE tblName SET colName = regexp_replace(colName, '^.*''.*$', '');
In practice, this would give the same effect as the simpler:
UPDATE tblName SET colname='' WHERE colname LIKE '%''%';
Your actual use case is much simpler: you want to replace all occurrences of a fixed string (', which will need to be quoted and escaped as '''') with another fixed string (the empty string, written ''). So you don't need any pattern matching at all, just straight replacement using replace:
UPDATE tblName SET colname=replace(colname, '''', '');
This will probably be faster if you limit it to rows that contain an apostrophe to begin with:
UPDATE tblName SET colname=replace(colname, '''', '') WHERE colname LIKE '%''%';
% is not an regexp character
try this
select regexp_replace(colName, $$'$$, '','g') from tblName;
($$ is use to surround your string instead of ' to simplify the query)
(,'g') is use to continue after the first quote is found.
UPDATE tblName SET colName = regexp_replace(colName, $$'$$, '','g');
I have input string something like :
1.2.3.4_abc_4.2.1.44_1.3.4.23
100.11.11.22_xyz-abd_10.2.1.2_12.2.3.4
100.11.11.22_xyz_123_10.2.1.2_1.2.3.4
I have to replace the first string formed between two ipaddress which are separated by _, however in some string the _ is part of the replacement string (xyz_123)
I have to find the abc, xyz-abd and xyz_123 from the above string, so that I can replace with another column in that table.
_.*?_(?=\d+\.)
matches _abc_, _xyz-abd_ and _xyz_123_ in your examples. Is this working for you?
DECLARE
result VARCHAR2(255);
BEGIN
result := REGEXP_REPLACE(subject, $$_.*?_(?=\d+\.)$$, $$_foo_$$);
END;
Probably this is enough:
_[^.]+_
and replace with
_Replacement_
See it here on Regexr.
[^.]+ uses a negated character class to match a sequence of at least one (the + quantifier) non "." characters.
I am also matching a leading and a trailing "_", so you have to put it in again in the replacement string.
If PostgreSQL supports lookbehind and lookahead assertions, then it is possible to avoid the "_" in the replacement string:
(?<=_)[^.]+(?=_)
See it on Regexr
In order to map match first two "" , as #stema and #Tim Pietzcker mentioned the regex works. Then in order to append "" to the column , which is what I was struggling with, can be done with || operator as eg below
update table1 set column1=regexp_replace(column1,'.*?(?=\d+.)','' || column2 || '_')
Then for using the another table for update query , the below eg can be helpfull
update table1 as t set column1=regexp_replace(column1,'.*?(?=\d+.)','' || column2 || '_') from table2 as t2 where t.id=t2.id [other criteria]
I need to remove the characters -, +, (, ), and space from a string in Oracle. The other characters in the string will all be numbers.
The function that can do this is REGEXP_REPLACE. I need help writing the correct regex.
Examples:
string '23+(67 -90' should return '236790'
string '123456' should return '123456'
Something like
SQL> ed
Wrote file afiedt.buf
1 with data as (
2 select 'abc123def456' str from dual union all
3 select '23+(67 -90' from dual union all
4 select '123456' from dual
5 )
6 select str,
7 regexp_replace( str, '[^[:digit:]]', null ) just_numbers
8* from data
SQL> /
STR JUST_NUMBERS
------------ --------------------
abc123def456 123456
23+(67 -90 236790
123456 123456
should do it. This will remove any non-digit character from the string.
regexp_replace is an amazing function, but it is a bit difficult.
You can use TRANSLATE function to replace multiple characters within a string. The way TRANSLATE function differs from REPLACE is that, TRANSLATE function provides single character one to one substitution while REPLACE allows you to replace one string with another.
Example:
SELECT TRANSLATE('23+(67 -90', '1-+() ', '1') "Replaced" FROM DUAL;
Output:
236790
In this example, ‘1’ will be replaced with the ‘1’ and ‘-+()‘ will be replaced with null value since we are not providing any corresponding character for it in the ‘to string’.
This statement also answers your question without the use of regexp.
You would think that you could use empty string as the last argument, but that doesn't work because when we pass NULL argument to TRANSLATE function, it returns null and hence we don’t get the desired result.
So I use REPLACE if I need to replace one character, but TRANSLATE if I want to replace multiple characters.
Source: https://decipherinfosys.wordpress.com/2007/11/27/removing-un-wanted-text-from-strings-in-oracle/
search for \D or [\-\+, ]and replace with empty string ''
regexp_replace is an amazing function, save a lot of time to replace alphabets in a alphanumeric string to convert to number.
I'm having trouble writing a regular expression (suitable for PHP's preg_match()) that will parse keyword='value' pairs regardless of whether the <value> string is enclosed in single or double quotes. IOW in both of the following cases I need to get the <name> and <value> where the <value> string may contain the non-enclosing type of quotes:
name="value"
name='value'
In Perl this is a regular expression that would work. It first matched for the start of the line then matches for one or more non = characters and sets them to $1. Next it looks for the = then the a non parentheses with a choice of matching for " or ' and sets that to $2.
/^([^=]+)=(?:"([^"]+)"|'([^']+)')$/
If you wanted it to match blank expressions like.
This=""
Replace the last two + with an * Otherwise this should work
Edit
As mentioned in the comments. Doug used...
/^\s?([^=]+)\s?=\s?("([^"]+)"|\'([^\']+)\')\s?/
This will match one optional white space on ether end of the input or value and he has removed the end of line marker.
/^(\w+?)=(['"])(\w+?)\2$/
Which will place the key in $1 and the value in $3.
A few years late, but since this question is highly ranked on google, and the answers didn't satisfy my needs, here's another expression:
(?<key>\w+)\s*=\s*(['"]?)(?<val>(?:(?!\2)[^\\]|\\.|\w)+)\2
This will match single or double quotes, taking into account escaped quotes, and unquoted values.
name = bar
name = "bar"
name = 'bar'
name = "\"ba\"r\""
It, however, does have a limitation in that if a value is missing the key from the next key/value pair is read. This can be addressed by using a comma separated list of key/value pairs.