regular expression conditional validation [closed] - regex

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
sample input text:
a) owner.table_name
b) table_name
can somebody suggest regular expression so that I get out put in such a way that i get text upto "." (if "." exists) and if "." doesnt exist then return empty
output:
a) owner
b) empty
regular expression so that I get out put in such a way that I get text from "." up to end (if "." exists) and if "." doesn't exist then return entire string
output:
a) table_name
b) table_name

The following regex should work:
(?:(\w+)\.)?(\w+)
It creates two capture groups (see this debuggex example to play around with it)
The first group will be empty, with the input table_name. For owner.table_name both groups have the appropriate values.

Here's a regex that would match both in one:
((?<owner>\w+)\.)?(?<table>\w+)
If the tool you are using allows named groups, the first group will be called "owner" and will be any word before a period. The second group will be called "table" and will be any word after a possible "owner".

Related

I wanted to remove particular pattern from a string in tcl [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
hreset_error_status_t2_reg1
htrans_flag_t1_reg2
hsize_flag_error_reg
check_hwdata_reg
I had to write a Tcl script using regex I have to remove error_status, flag, flag_error, and check in the following strings. It has to search the following keywords in the file and have to remove the mentioned keywords in a string.
To remove all those patterns from a string, use regsub -all. If you don't need to use the power of regular expressions, then string map works well too.
set my_str "hreset_error_status_t2_reg1 htrans_flag_t1_reg2 hsize_flag_error_reg check_hwdata_reg"
set my_regex {error_status_|flag_(error_)?|check_}
regsub -all $my_regex $my_str ""
--> hreset_t2_reg1 htrans_t1_reg2 hsize_reg hwdata_reg
or
set my_map {
error_status_ ""
flag_error_ ""
flag_ ""
check_ ""
}
string map $my_map $my_str
--> hreset_t2_reg1 htrans_t1_reg2 hsize_reg hwdata_reg
Note that I put flag_error_ before flag_ in $my_map so that only the flag_ characters in flag_error weren't removed.

Regex Match in between Strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I using Regex to extract a pattern which is of the form [a-zA-z][0-9]{8} Ex:K12345678
I need to extract this pattern from a string and this pattern should be matched properly
I tried the below but my testcase if failing for this scenario
This is my Regex /[a-zA-Z][0-9]{8}/g
phonne number:9978434276K12345678:My pattern
For this scenario it is failing.
My Sample Code
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);
The expected output is K12345678.The Regex which I wrote does not handle this.
You can use String.match(Regex)
"9978434276K12345678".match(/[a-zA-Z][0-9]{8}/)
It returns an array of 4 elements: [String coincidence, index: Number, input: String, groups: undefined]
just stay with the element 0: coincidence and 1: index of the match.
and use this just to check that the string matches at least one
/Regex/.test(String)
/[a-zA-Z][0-9]{8}/.test("9978434276K12345678")
It will return true or false
USE expression without quotation marks
const expression = /[a-zA-Z][0-9]{8}/;
const content = "phone number:9978434276K12345678:My pattern"
let patternMatch = content.match(expression);

regular expression to leave only first and last character of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i'm using a postgres database and I'd like to show certain data leaving just the first and last character of a string (with variable length and containing spaces and special characters); I'd like to replace characters in between with dots '.' or i.e. 'x'
Could you please suggest me the regexp_replace() syntax to use?
Thanks in advance
Instead of replacing part of the existing string, you could just construct a new one from pieces - the first character, the middle, and the last character.
SELECT left(str, 1) || repeat('.', length(str) - 2) || right(str, 1);
As #kaveh pointed out, that won't work unless the string has at least two characters. This uglier one should work for those cases too:
SELECT CASE
WHEN length(str) > 2 THEN left(str, 1) || repeat('.', length(str) - 2) || right(str, 1)
ELSE str END;

Regex pattern match string irrespective of order [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a comma-separated string and wanted to check if the string has all words irrespective of order using REGEX pattern.
Example: Original String = "help, assist, aid";
Use Cases:
"help, assist, aid" -> return true
"aid, assist, help" -> return true
"aid, help,assist" -> return true
"aid, help" -> return false
"aid, help,support" -> return false
Can someone help me in writing the regex pattern for this use case?
You could use three positive lookahead assertions:
^(?=.*\bhelp\b)(?=.*\baid\b)(?=.*\bassist\b).*$
Each of the three lookaheads is actually zero width, and so would all be evaluated once at the very start of the pattern.
Demo

Check a string has an integer in the last part using Regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have few lines
Eg:
word1-word2-word3-1
word1-word2-word3-23
word1-word2-word3-dr
word1-word2-word3-13-4
word1-word2-word3-drg
word1-word2-word3-word4-4
From the above lines i need the following lines by using Regex
word1-word2-word3-1
word1-word2-word3-23
word1-word2-word3-word4-4
ie. The string ending with a "-{int}"
You can use the pattern -\d+$
The pattern means that we are look for;
-\d+ - hyphen and one or more digits
$ Asserts that we are at the line end.
I don't know what language you are writing this in but assuming you are doing it in Javascript. This pattern can be proved using the code below.
"use strict";
var values = [
"word1-word2-word3-1",
"word1-word2-word3-23",
"word1-word2-word3-dr",
"word1-word2-word3-13-4",
"word1-word2-word3-drg",
"word1-word2-word3-word4-4"
];
values.forEach(value => {
if (/.*-(?!\d+-)\w+-\d+$/.exec(value)) {
console.log(value + " has - integer at the end!");
};
});