Regex issue on SQL - mysql

Why do I get 0 when running this expression?
SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1+[ ,.-/\]*7+[ ,.-/\]*0+[ ,.-/\]*0+[ ,.-/\]*9+[ ,.-/\]*0+[ ,.-/\]*2+[ ,.-/\]*8+[ ,.-/\]*4+[ ,.-/\]*2+[ ,.-/\]*3+';
I need to get true, when the text contains the specified number (17009028423). There can be symbols ,.-/\ between digits.
For example, if I have number 17009028423, I need get true when in text is:
1700-902-8423
17-00,902-84.23
170/09-0.28\423
1700..902 842-3
17,.009028 4//2\3
etc.
Thanks.

There are two problems with your regular expression. First is that backslash in \] escapes the special meaning of ] to denote a character class. You need to escape your backslash: \\]. Another problem is that - denotes a range [ and ] (e.g. [a-zA-Z]). You need to escape that too or put it at the end like [a-zA-Z-] (as #tenub said). Plus the backslashes should be escaped themselves, which makes:
SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1[ ,./\\\\-]*7[ ,./\\\\-]*0[ ,./\\\\-]*0[ ,./\\\\-]*9[ ,./\\\\-]*0[ ,./\\\\-]*2[ ,./\\\\-]*8[ ,./\\\\-]*4[ ,./\\\\-]*2[ ,./\\\\-]*3'
You can check for yourself.
I also removed + signs in case you want to match each number only once.

Related

Regex expression for Changing close brackets to open brackets

I have a JSON text full of these:
"order": "Commande",
"#order": }
"description": "order word",
"type": "texte"
},
As you can see, There is an error in front of "#Order:" which } is used instead of {
How can I replace all of them with open bracket without changing }, at the end of objects? (I mean I need the regex expression to use it in search of my text editor)
^\s\}{1}\s didn't work
Maybe this can help you
(?<=\"#\w+\":\s+)}
https://regex101.com/r/A4Yv6X/1
Your ^\s\}\s pattern matches a whitespace, } and whitespace at the start of string. while the } you want to replace is at the end of string (or line).
You may consider using \}$, or \}(?=\s*$), or \}(?=\h*$) patterns to match } only at the end of a string/line (where $ is the end of string/line, (?=\s*$) matches a location that is immediately followed with any 0 or more whitespaces and then the end of a string/line, \h just only allows horizontal whitespaces).
However, if there is a colon before the } you need to replace you may consider a more sophisticated pattern like
(:\h*)\}(\h*$)
(:[^\S\r\n]*)\}([^\S\r\n]*$)
Replace with $1{$2 (or \1{\2 depending on the environment).
See the regex demo. Details:
(:\h*) - Capturing group 1 ($1): a colon and zero or more horizontal whitespaces
\} - a } char
(\h*$) - Group 2 ($2): zero or more horizontal whitespaces.
Note that [^\S\r\n] is a rough equivalent to \h, it matches any whitespace char but CR and LF chars.
Try this, which matches brackets at the ends of lines
(?m)\}$
See live demo.
Depending on your input, this might be enough.

HTML Input Pattern Comma and Letters

What is the correct pattern for a text input to only allow uppercase letters, lowercase letters, and commas?
I know that this is correct for the letters:
pattern="[a-zA-Z]"
but I dont know how to allow commas.
Thanks for any help!
Short answer:
pattern="^[a-zA-Z,]*$"
A couple of comment:
* means zero or more characters which means this patter will allow empty fields as well. If you want to guarantee that it will contain at least one character, use + instead of *.
^ means beginning of the string and $ is the end. If you don't use them then something like this would be possible "!#123asdSDADS,,,21312312(2"

Sublime Text regex to find and replace whitespace between two xml or html tags?

I'm using Sublime Text and I need to come up with a regex that will find the whitespaces between a certain opening and closing tag and replace them with commas.
Example: Replace white space in
<tags>This is an example</tags>
so it becomes
<tags>This,is,an,example</tags>
Thanks!
You have just to use a simple regex like:
\s+
And replace it with with a comma.
Working demo
This will find instances of
<tags>...</tags>
with whitespace between the tags
(<tags>\S+)\W(.+</tags>)
This will replace the first whitespace with a comma
\1,\2
Open Find and Replace [OS X Cmd+Opt+F :: Windows Ctrl+H]
Use the two values above to find and replace and use the 'Replace All' option. Repeat until all the whitespaces are converted to commas.
The best answer is probably a quick script but this will get you there fairly fast without needing to do any coding.
You can replace any one or more whitespace chunks in between two tags using a single regular expression:
(?s)(?:\G(?!\A)|<tags>(?=.*?</tags>))(?:(?!</?tags>).)*?\K\s+
See the regex demo. Details
(?s) - a DOTALL inline modifier, makes . match line breaks
(?:\G(?!\A)|<tags>(?=.*?</tags>)) - either the end of the previous successful match (\G(?!\A)) or (|) <tags> substring that is immediately followed with any zero or more chars, as few as possible and then </tags> (see (?=.*?</tags>))
(?:(?!</?tags>).)*? - any char that does not start a <tags> or </tags> substrings, zero or more occurrences but as few as possible
\K - match reset operator
\s+ - one or more whitespaces (NOTE: use \s if each whitespace must be replaced).
SublimeText settings:

Regex matching Google Cache url (matching entire href parameter when it contains a word)

Disclaimer: I know that html and regex should not stand together, but this is an exceptional case.
I need to parse Google Search results and extract cache urls. I have this in the page:
<a href="/url?q=http://webcache.googleusercontent.com/search%3Fq%3Dcache:
gsNKb7ku3ewJ:somedata&ei=MyIIUtrZAcPX7AaVzIHwDg&ved=0CB8QIDAC&usg
=AFQjCNGcnWfdzQiTKwyAMmI-M-xzxII5Ag">Cached</a>
I tried simple stuff like: href=[\'"]?([^\'" >]+) but it is not what I need. I want to extract a single parameter (q) from the href. I need to get:
http://webcache.googleusercontent.com/search%3Fq%3Dcache:gsNKb7ku3ewJ:somedata
So everything between "url?q=" and first "&", when the contents contain word "webcache" in it.
If your language supports positive look-behinds:
(?<=q=).*?(?=[&"])
Otherwise match group \1 with this expression:
(?:q=)(.*?)(?=[&"])
Explanation:
.*? is the body of our expression. Just match everything, but don't be greedy!
(?<=q=) is a positive look-behind, which says "q=" should come before the match
(?=[&"]) is a positive look ahead, which says "either & or a quote should come after the match"
Because we make it not greedy with the ?, it'll stop at the first quote or ampersand. Otherwise it'd match all of the way to the closing quote.
Use a look behind before, and a look ahead at the end to assert the surrounding text, and include the keyword in the regex:
(?<=url\?q=)[^&]*webcache[^&]*(?=&)
Using [^&]* ensures that the keyword occurs before an & - within the target string.

Regex all uppercase with special characters

I have a regex '^[A0-Z9]+$' that works until it reaches strings with 'special' characters like a period or dash.
List:
UPPER
lower
UPPER lower
lower UPPER
TEST
test
UPPER2.2-1
UPPER2
Gives:
UPPER
TEST
UPPER2
How do I get the regex to ignore non-alphanumeric characters also so it includes UPPER2.2-1 also?
I have a link here to show it 'real-time': http://www.rubular.com/r/ev23M7G1O3
This is for MySQL REGEX
EDIT: I didn't specify I wanted all non-alphanumeric characters (including spaces), but with the help of others here it led me to this: '^[A-Z-0-9[:punct:][:space:]]+$' is there anything wrong with this?
Try
'^[A-Z0-9.-]+$'
You just need to add the special characters to the group, optionally escaping them.
Additionally if you choose not to escape the -, be aware that it should be placed at the start or the end of the grouping expression to avoid the chance that it may be interpreted as delimiting a range.
To your updated question, if you want all non-whitespace, try using a group such as:
^[^ ]+$
which will match everything except for a space.
If instead what you wanted is all non-whitespace and non-lowercase, you likely will want to use:
^[^ a-z]+$
The 'trick' used here is adding a caret symbol after the opening [ in the group expression. This indicates that we want the negation of the match.
Following the pattern, we can also apply this 'trick' to get everything but lowercase letters like this:
^[^a-z]+$
I'm not really sure which of the 3 above you want, but if nothing else, this ought to serve as a good example of what you can do with character classes.
I believe you are looking for (one?) uppercase-word match, where word is pretty much anything.
^[^a-z\s]+$
...or if you want to allow more words with spaces, then probably just
^[^a-z]+$
You just need to put in the . and -. In theory, you don't need to escape because they are inside the brackets, but I like to to remind myself to escape when I have to.
'^[A-Z0-9\.\-]+$'
Try regular expression as below:
'^[A0-Z0\\.\\-]+$'