In a string such as:
CustomerDisplayVersionNumber : Version 12.3 (build 567.89)
How can I, using a regex, return true if the version number is exactly 12.3
There is the slight, but real possibility that the version number might contain a service pack version. For example
CustomerDisplayVersionNumber : Version 12.3.4 (build 567.89)
Which leads me to believe it will be safer to check for [space]Version 12.3.nnnn[space].
Regex "\sVersion (\d+\.\d+(\.\d+)?)\s" will satisfy all provided examples
#('CustomerDisplayVersionNumber : Version 12.3 (build 567.89)', 'CustomerDisplayVersionNumber : Version 12.3.4 (build 567.89)', 'CustomerDisplayVersionNumber : Version 12.3.9999 (build 567.89)') | % { [regex]::Match($_, "\sVersion (\d+\.\d+(\.\d+)?)\s").Success }
use this regex \s+Version \d+\.\d+\.\d+\s+
Related
I have a source on which I have no control and I want to filter out all string which have some characters in it.
For example Out of these:
9
8.1.0
5.0
9.0
5.1
8.0.0
7.0 (cdfsdsdsd)
5.0.2
8
7.0.1
7.1
6.0
7.0
Over 32323
7.0 rdx K9 bnsm
9.2.3
8.oo
pp
unknown
8.0_vgfe10051988
6.0.1
8.0.0-derv10051988
9.1
9.0.0
8.0.1
7.0_xccv10051988
7.1.3
10.0
7.0.X.1.C
8.0.0_vged10051988
4.4.4
7.1.2
7.0 [NKL 24 | ABC]
8.1
7.1.1
5.1.1
7.0_Jgrd10051988
9.XXX
9.0.1
8.0
5.0.1
8.1.1
10
Out of these I need only those Strings with only digits and .
9
8.1.0
5.0
9.0
5.1
8.0.0
5.0.2
8
7.0.1
7.1
6.0
7.0
9.2.3
6.0.1
9.1
9.0.0
8.0.1
7.1.3
10.0
4.4.4
7.1.2
8.1
7.1.1
5.1.1
9.0.1
8.0
5.0.1
8.1.1
10
I have tried many regex, but nothing seems to be generic enough,
This regex is giving [0-9]*.?[0-9] Strings too.
The one I have got working is ^(\*|\d+(\.\d+){0,2}(\.\*)?)$, but this is not POSIX.
How do I get a POSIX which also works on Redshift?
By taking a look of the Amazon document, it seems POSIX ERE is supported by the Redshift. Then would you please try:
^[[:digit:]]+(\.[[:digit:]]+)*$
Your regex works, you just need to use double backslashes in the string literal.
According to the Amazon Redshift "POSIX Operators" documentation,
Amazon Redshift supports the following Perl-influenced operators in regular expressions. Escape the operator using two backslashes (‘\\’).
So, you may use
'^(\\*|\\d+(\\.\\d+){0,2}(\\.\\*)?)$'
The simplest is:
^[.0-9]+$
If you don't have support for extended regex, you can do:
^[.0-9][.0-9]*$
I ran this command on your input and output and got an empty diff:
$ diff <(grep -P '^[.0-9]+$' input) output
$ echo $?
0
On your specific input, even ^[.0-9]*$ would work.
Note, however, that there's a difference between "Strings with only digits and ." and "version string". The simple regex will also catch inputs like:
1..2
..
.
0...
.1
If that's not a problem, you can use the simple regex.
I have a C++ project using QMake. I'm trying to set some compiler options based on a simple test of which Linux distro is running, but the test does not pass. My qmake file contains:
OSDISTRO = $$(cat /proc/version)
contains(OSDISTRO, "Ubuntu"): {
message(Found ubuntu)
}
I tested the regex from the command line and it works!
cat /proc/version | pcregrep "Ubuntu"
Linux version 4.18.0-20-generic (buildd#lcy01-amd64-020) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019
Is there something special about the regex syntax in qmake? Any reason why this isn't working?
For the RegEx:
This works for me:
OSDISTRO = $$system(cat /proc/version)
contains(OSDISTRO, .*Ubuntu.*){
message("Found Ubuntu")
}
Note:
The match is case sensitive.
You can use .*[uU]buntu.* for example to match ubuntu and Ubuntu.
Explanation why your solution does not work:
The QMake function contains works with lists of values.
So, the execution of you solution will be like this:
1) First instruction OSDISTRO = $$(cat /proc/version):
QMake will execute $$system(cat /proc/version).
Then the result is splitted (by space as separator) to list of values. OSDISTRO will contain this list.
Assuming that the result is as yours. The result of the first instruction is like this:
OSDISTRO = "Linux" "version" "4.18.0-20-generic"....
2) Second instruction contains(OSDISTRO, "Ubuntu") : message(Found ubuntu):
QMake will search if the variable OSDISTRO contains the value Ubuntu and display the message Found ubuntu if success.
Here in your case, QMake will never find Ubuntu, cause the value which contains it is like this (Ubuntu 7.3.0-16ubuntu3) and QMake search only the value Ubuntu.
Hope it helps you.
I am trying to Get gdb pretty-print working in eclipse, it seems to be working for all stl elements and containers baring std::string
basically if i have a vector like:
std::vector<std::string> m_vec = {"hello" , "world"};
each element of the vector shows a string and it shows the contents like "hello" and "world".
but if i have a code like
std::string m_string = "hello world";
m_string shows up empty, even though i can do string operations on the contents. Not sure why only strings alone is causing as issue with pretty print.
Any help/pointers would be much appreciated.
Edited:
Few more details regarding the setup:
IDE: Eclipse Luna 4.4.2
Compiler: Cygwin g++ 5.4.0
Debugger: Cygwin gdb 7.10.1
phython : Cygwin phython 3.6
Update: Don't know how exactly, but windows restart solved it, can see string now in preety-print.
For adding support for gdb pretty-printing in cygwin you need the following file available:
/usr/share/gdb/auto-load/usr/bin/cygstdc++-6.dll-gdb.py
It is available in gcc-debuginfo-5.4.0-1 package in cygwin package installer.
Note: This might break once packages get updated in Cygwin repository. So you will need to again locate this file in the Cygwin package search.
Update: Check the version of python interpreter supported by gdb by running the following commands in the gdb console:
(cygwin console) $ gdb
(gdb) python
>import sys
>print(sys.version)
>end
You should see output something like this(in my case it defaults to python2.7 interpreter):
2.7.13 (default, Mar 13 2017, 20:56:15)
[GCC 5.4.0]
Faced with the problem of parsing version of clang for different vendors clang --version|head -1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) => 3.5
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512 => 3.4.1
clang version 3.5.0 (tags/RELEASE_350/rc2) => 3.5.0
Now have this regular expression
match: (clang version|based on LLVM)\ (\d+)(\.\d+)(\.\d+)?
to \2\3\4
I need to exclude (clang version|based on LLVM) from the result match() - \1.
Your question looks like you're expecting CMake's regex handling to be Perl-like, but it's pretty different.
In CMake syntax, the following should do what you want:
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
execute_process(COMMAND clang --version COMMAND head -1 OUTPUT_VARIABLE ClangVersionLine)
string(REGEX MATCH "(clang version|based on LLVM) ([0-9]\\.[0-9]\\.?[0-9]?)" UnusedVar "${ClangVersionLine}")
set(ClangVersion "${CMAKE_MATCH_2}")
message("ClangVersion - ${ClangVersion}")
endif()
Ok, since the tool you use is based on PCRE, this pattern should work:
(?m)(?:^Apple .*)?\K \d+(?:\.\d+){1,2}
The (?m) is not useful in real life because you only test one line, so you can remove it.
The \K removes all that have been matched on the left from the match result.
If you use another language/tool where the \K feature is not available you can use a capturing group to extract the information you want from the whole match result:
(?:^Apple .*)? ([0-9]+(?:[.][0-9]+){1,2})
With cmake:
string(REGEX REPLACE "(^Apple .*)? ([0-9]+([.][0-9]+){1,2}).*" "\\2" CLANG_VERSION "${_clang_version_info}")
I am trying to kernel & gcc version by reading /proc/version file using Perl. But I am not sure but the regex to match the versions. I tried something like this
/Linux version (\d+)*gcc version(\d+)*/
But its not working. Thanks in advance. I am newbie to Perl. And the contents of version is
Linux version 2.6.32-21-generic (buildd#rothera) (gcc version 4.4.3
(Ubuntu 4.4.3-4ubuntu5) ) #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010
Try this:
$version =~ /Linux version ([\d.-]+)-\D.*gcc version ([\d.]+) /;
print "Linux version: $1\ngcc version: $2\n";
The output:
Linux version: 2.6.32-21
gcc version: 4.4.3
This regex will work for you
/Linux version ([\w.-]*).*?gcc version ([\w.]*)/
First captured group will have linux version 2.6.32-21-generic and second captured group will have gcc version 4.4.3. If you do not want to capture generic then use \d instead of \w.
m{Linux version\s(\S+).*gcc version\s(\S+)} and print "$1\n$2\n"