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 1 year ago.
Improve this question
I got a string representing users data.
What is the proper regex to extract domain in this string?
I know that I have to find all strings with 2 characters matching the condition that it comes after the last "." after a "#".
However I still failed to implement it.
import re
regex = r"#.+\.([a-z]{2}),"
your_string = ("001,Francisca,Dr Jhonaci,jhonadr#abc.com,32yearsold,120.238.225.0\n"
"002,Lavenda,Bocina,lavenboci#banck.ac.uk,50yearsold,121.186.221.182\n"
"003,Laura,Eglington,elinton#python.co.jp,26yearsold,36.55.173.63\n"
"004,Timo,Baum,timobaum#tennis.co.cn,22yearsold,121.121.110.10")
matches = re.finditer(regex, your_string, re.MULTILINE)
for match in matches:
result = match.group(1)
print(result)
The comma seems to be the delimiter in the string.
To not cross-matching a comma (to prevent matching too much), and also not cross-matching a second # char you can use a negated character class starting with [^
If the entry can also be at the end of the string, you can assert either a , or the end of the string.
#[^#,]*\.([A-Za-z]{2})(?=,|$)
Regex demo
import re
regex = r"#[^#,]*\.([A-Za-z]{2})(?=,|$)"
s = ("001,Francisca,Dr Jhonaci,jhonadr#abc.com,32yearsold,120.238.225.0\n"
"002,Lavenda,Bocina,lavenboci#banck.ac.uk,50yearsold,121.186.221.182\n"
"003,Laura,Eglington,elinton#python.co.jp,26yearsold,36.55.173.63\n"
"004,Timo,Baum,timobaum#tennis.co.cn,22yearsold,121.121.110.10")
print(re.findall(regex, s, re.M))
Output
['uk', 'jp', 'cn']
Use the comma after the email instead of the last point.
Using this regex
#.+\.(\w+)(?<!com),
the capturing group will contain the info that you want.
Related
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 5 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
How can I add brackets to each word of a string which contains multiple words in SQL? For example, I have:
string example = 'word1,word2,word3,word4,word5'
How can I convert it to something like this:
string example = '[word1],[word2],[word3],[word4],[word5]'
We can use a combination of REPLACE() and string concatenation:
SELECT val, CONCAT('[', REPLACE(val, ',', '],[') , ']') AS output
FROM yourTable;
On MySQL 8+, we can use a regex replacement:
SELECT val, REGEXP_REPLACE(val, '([^,]+)', '[$1]') AS output
FROM yourTable;
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
<a href='#' onclick='returnUrl = /url=(https?:\/\/.+)/.exec(location); if(returnUrl)location.href = returnUrl[1];else location.href = "/"'>Back to Blog</a>
Can anyone explain the above code in detail?
In this example:
you trying to apply regex url=(https?:\/\/.+) to current location, afaik "location" can't return something starts with "url=", but i can mistake.
regex.exec answer (it's Array) you insert in returnUrl var
if there is some entries in given array, then you insert second entry in location.href (why second?: Why capturing group results in double matches regex)
if there is no entries in given array, then you insert '/' in location.href
inserting a value into a 'location' variable is equivalent for navigating to inserted url
regex explanation from regex.com
url= matches the characters url= literally (case sensitive)
1st Capturing Group (https?:\/\/.+)
http matches the characters http literally (case sensitive)
s?
matches the character s literally (case sensitive)
: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
.+
matches any character (except for line terminators)
your example in pseudocode:
if there is matches for /url=(https?:\/\/.+)/ in location
then redirect to this match (in group)
else redirect to '/'
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 4 years ago.
Improve this question
I have a list of various unrelated strings on separate lines in Notepad++.
127.0.0.1
badwebsite.com
dontgohere.com
How do I use Notepad++ to create something like the below?
{"blocked":true,"flagged":true,"string":"127.0.0.1","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"127.0.0.1","readOnly":null,"id":null,"category":null,"enabled":null}
I basically want to copy the contents of the line to use again. First, I need to prepend it with this:
{"blocked":true,"flagged":true,"string":"
Next is the original line contents:
127.0.0.1
Then, I would insert the following content:
","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"
Afterwards, the original contents of the line would be repeated:
127.0.0.1
Finally, I would append it with the following:
","readOnly":null,"id":null,"category":null,"enabled":null}
I do know that I would use the replace feature with regular expression to search for ^ and replace with whatever I want at the start of the line and I would search for $ and replace with whatever I want at the end of the line. Unfortunately, I don't know how to repeat the contents of a line while inserting a string.
In Notepad++ you can use Replace with regular expression enabled and find:
^(.+)$
which finds a set of characters between the start and end of a line and stores them in a capture group which we can refer to using $1 in the replacement string:
{"blocked":true,"flagged":true,"string":"$1","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"$1","readOnly":null,"id":null,"category":null,"enabled":null}
Output:
{"blocked":true,"flagged":true,"string":"127.0.0.1","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"127.0.0.1","readOnly":null,"id":null,"category":null,"enabled":null}
{"blocked":true,"flagged":true,"string":"badwebsite.com","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"badwebsite.com","readOnly":null,"id":null,"category":null,"enabled":null}
{"blocked":true,"flagged":true,"string":"dontgohere.com","javaClass":"com.untangle.uvm.app.GenericRule","name":null,"description":"dontgohere.com","readOnly":null,"id":null,"category":null,"enabled":null}
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 6 years ago.
Improve this question
I am parsing an excel file and adding the values im getting into a database. But one of the columns I am parsing can be either a float or a string. Like such: 0,45 or Contact Support. What is the best way for me to add this as it is, to my database? Or more how should I format my database column for this?
use varchar datatype(in MySQL) for the column you want to parse and you can save both the integer and character values.
I recommend you create a float type column and a varchar column. Write your parser so that valid float values are filled to the float column and the rest to the varchar one. This will allow you to both sort your database by the value of the float type column and run queries on fields that are missing that value: SELECT * FROM mytable WHERE myfloat IS NULL
first am with Kaivosukeltaja answer.
but if you can't do that, make a varchar column to save the strings or doubles.
BTW when you get data from a database all the data comes in string format then you can cast it to any type you want.
if you are using PHP:
$x = $row['string_double_column'];
if(is_numeric($x))
return doubleval($x);
else
return $x;
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 9 years ago.
Improve this question
MySQL does not support the Negative Lookahead. How can I find the result not containing a string using REGEXP.
I am using 'NOT REGEXP' but the result is unexpected.
there is a 'Content' column in my table,i want to find the rows which the Content column contains '' label,but i still want some src to be excluded.
here is the sql:
Content REGEXP '.' AND Content NOT REGEXP '.(test.mywebsite1.com/|img.mywebsite.com/face/|test.mywebsite.com/phoneIcon.jpg).*'
but when the Content contain both and it works unexpected;
Test your REGEXP on a known set, get that working, and verify it is working.
Then add the NOT to get the boolean inverse.
Note that a MySQL boolean expression will return one of three possible values: TRUE, FALSE and NULL.
And note that NOT expr will also return one of three possible values: TRUE, FALSE and NULL.
When expr returns NULL, then NOT expr will also return NULL.
It's not really productive to attempt to provide any other assistance, absent an actual question and more details of what you are attempting to do.