Hi I have a string in oracle like this:
temp_a, temp_b, temp_c
What I want to get is :
e.temp_a, e.temp_b, e.temp_c
So I want to put an "e." before every part in this string
I searched in internet and I found examples to split the string or to replace simpler strings but nothing to guide me through my problem
select regexp_replace('temp_a, temp_b, temp_c',
'([a-zA-Z0-9_]+)(,?)',
'e.\1\2') from dual;
This should work.
I just noticed you're specifically asking for regex, but for what it's worth I would probably do it like this:
rtrim( replace( 'e.'||your_string, ', ', ', e.'), 'e.')
Related
we have ranges like this
"0,5, 0,5"
"0,112, 0,118"
and want to split by the second comma.
Any idea?
You can update the regex you split by with comma then a space after.
select regexp_split_to_array('0,112, 0,118', ', ')
demo:db<>fiddle
Supposing, there ist always at least one space after the second comma and none after the others, you could use this for the split regex:
SELECT
regexp_split_to_array(ranges, ',\s+')
FROM
t
This returns an array like {"0,5","0,5"}.
You can split both ranges into columns using a subquery:
SELECT
r[1],
r[2]
FROM (
SELECT
regexp_split_to_array(ranges, ',\s+') as r
FROM
t
) s
Edit:
TO wants to get everything after the second comma. So you need a regex for splitting, which finds the nth (here n = 2) occurrence of a comma:
(?:(^.*?,.*)),
This can be used to query the required data:
demo:db<>fiddle
SELECT
(regexp_split_to_array(ranges, '(?:(^.*?,.*)),'))[2]
FROM
t
Use regexp_replace:
select regexp_replace('0,112, 0,118', '.*,\s+', '') as foo;
Output:
foo
-------
0,118
(1 row)
Thank you all for the quick answers. It finally worked by using this
regexp_matches(your_string_value, '\d+[,|.]\d+|\d+','g'))[1]
This helped me getting rid of all unnecessary characters within the values + delivered me back the second value in the range.
I have several string like
kw_CS_TABLE__FC29-001::details=MIN_CAT::title=xxxx
kw_CS_TABLE__FC29-002::details=CAT to NSE
kw_CS_TABLE__FC29-003::details=HAZMIN::
I want to retrieve only the details string (MIN_CAT, CAT to NSE, HAZMIN).
I use the regex (?<=::details=)(.*)(?=::), it looks fine for the first and 3rd case. But it fails for the second case.
I am struggle with the recognition of the end of the string. I use the |$ command, but in this case, I retrieve all the sentence up to the end of the file.
(?<=::details=)(.*)(?=::|$)
kw_CS_TABLE__FC29-001::details=MIN_CAT::title=xxxx
returns > MIN_CAT::title=xxxx
I have a lots of difficulties to understand the regex concepts, especially because I use it only for some specific case. I read several tutorials and posts, but nothing solve my problem.
Thanks
Without regex
Private Function GetDetailsFrom(line As String) As String
Return line.Split({"::"}, StringSplitOptions.None).
Where(Function(item) item.StartsWith("details")).
Select(Function(detail) detail.Split({"="c}).LastOrDefault()).
FirstOrDefault()
End Function
Usage
Dim lines As String() =
{
"kw_CS_TABLE__FC29-001::details=MIN_CAT::title=xxxx",
"kw_CS_TABLE__FC29-002::details=CAT to NSE",
"kw_CS_TABLE__FC29-003::details=HAZMIN::"
}
Dim details = lines.Select(AddressOf GetDetailsFrom)
Console.WriteLine(string.Join(Environment.NewLine, details))
' MIN_CAT
' CAT to NSE
' HAZMIN
I have a string like this in matlab.
str='42 21 S'
How could I convert it into the following form?
str='42.21'
What I tried with regexprep() is the following:
regexprep(str,'S','');
regexprep(str,' ', '.')
which leaves me with this
str='42.21.'
This ought to do the trick, Matlab is not great with strings though so there's likely to be all sorts of ways to do it, not just using regexp/regexprep:
regexprep(regexp('42 21 A','\d+\s\d+','match'),'\s','.')
The regexp removes the space and the S at the end, and then the regexprep replaces the space with a period.
For simple replacements you don't have to use regexprep. You can use the much simpler strrep:
str = strrep(str, ' S', '');
str = strrep(str, ' ', '.');
If you require more general replacement rules, you should use regexprep, like David's answer for example.
I know this topic was discussed multiple times, I looked at multiple posts and answers, but could not find exactly what I need to do.
I am trying to search the string, that has multiple values of varchar2 separated by ':', and if the match is found on another string, delete part of the string that matched, and update the table with the rest of the string.
I wrote the code using combination of str and instr functions, but looking for more elegant solution using regexp, or collections.
For example, the input string looks like this: ('abc:defg:klmnp). Need to find for example the piece of the string (could be at any position), and remove it, that result would look like this: (abc:klmnp)?
EDIT - copied from comment:
The input string (abc:defg:klmn:defgb). Let's say I am looking for defg, and only defg will have to be removed, not defgb. Now, like I mentioned before, next time around, I might be looking for the value in position 1, or the last position. So the desired part of the string to be removed might not always be wrapped in ':' from the both sides, but depending where it is in the string, either from the right, or from the left, or from both sides.
You can do this with a combination of LIKE, REPLACE and TRIM functions.
select trim(':' from
replace(':'||your_column||':',':'||search_string||':',':')
) from table_name
where ':'||your_column||':' like '%:'||search_string||':%';
Idea is,
Surround the column with colons and use LIKE function to find the match.
And on such matched rows, use REPLACE to replace the search string along with surrounding colons, with a single colon.
And then use TRIM to remove the surrounding colons.
Demo at sqlfiddle
EDIT (simplified) Perhaps this is what you need:
SELECT REGEXP_REPLACE(REPLACE('abc:defg:klmnop', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('defg:klmnop:abc', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('abc:klmnop:defg', ':defg:', ':'), '(^defg:|:defg$)', '')
, REGEXP_REPLACE(REPLACE('abc:klmnop:defgb:defg', ':defg:', ':'), '(^defg:|:defg$)', '')
FROM DUAL
;
which removes defg from start, middle, and end, and ignores defgb, giving:
abc:klmnop
klmnop:abc
abc:klmnop
abc:klmnop:defgb
And to update the table, you could:
UPDATE my_table
SET value = REGEXP_REPLACE(REGEXP_REPLACE(value, ':defg:', ':'), '(^defg:|:defg$)', '')
-- WHERE REGEXP_LIKE(value, '(^|.*:)defg(:.*|$)')
WHERE value LIKE '%defg%'
;
(though that final regex for the where may need to be tweaked to match, hard to test...)
I am trying to scrub some address catalog which was originally user input for later processing. Having removed numbers and special characters, now I have strings like:
ST SOMETHING SOMEWHERE H BL D S
So, ST stands for street, H house and so on.. Not trying to be super precise here, so I figured just removing anything under 3 chars would be enough for now, but I can't seem to find the correct regexp in Oracle. Have tried:
SELECT
regexp_replace('ST SOMETHING SOMEWHERE H BL D S','[\ ^]\w{1,3}[\ $]', ' ')
FROM dual;
which yields:
ST SOMETHING SOMEWHERE BL S
and I wish I had only
SOMETHING SOMEWHERE
could someone lend me a hand?
(I'm on oracle 11g)
try
SELECT
regexp_replace(
'<raw string>'
,'(^|[[:space:]])([^[:space:]]{1,3}[[:space:]]+)*[^[:space:]]{1,3}([[:space:]]|$)'
, ' '
)
FROM dual;
hope that will suit your needs, regards, carsten