How to ignore case in regex - regex

I want to check if a string contains two words "hello world". I am using something like this:
str = " aa bbb hEllo accc woRld"
str.matches( "(.*)" + "hello" + "(.*)" + "world" + "(.*)" );
How do I execute this regular expression as case-insensitive?

Try and put the case-insensitive modifier (?i) at the start of the regex:
str.matches( "(?i)(.)" + "hello" + "(.)" + "world" + "(.*)" );

Typically there is a flag that you can set. For many languages such as PHP/JS you would write your regex like: /REGEX/i with the i after your delimiters.

Perhaps this:
str.matches( "(.*)" + "([hH])" + "([eE])" + "([lL])" + "([lL])" + "([oO])" + "(.*)" + "([wW])" + "([oO])" + "([rR])" + "([lL])" + "([dD])" + "(.*)" );

I think there is a more efficient way than this awnser but anyway...
You can use the operator | (or) for each letter of both "hello" and "world" strings.
For instance with hello :
(H | h)(E | e)(L | l){2}(O | o)
Which means H or h then E or e then L or l (2 times) then O or o
I did not test this, but hope it will help you.

You can just lower case the string and compare it.
str = "dffdfHellodasfWorld"
re.findall("(.*)" + "hello" + "(.*)" + "world" + "(.*)", str.lower())
This is in python BTW.
Notice the str.lower()

Related

" java regex" match words with numbers and specials caracters

I have this regex :
"([ ]?[a-zA-Z]{3,})"
but when I try match these words :
"sol perro idea \ncaballo\ndo7\ntres\n tr_es\n8cuatro\ncinco.\n3pesos\n$dollar$\nccc\ncoton\nH7T\n chien#\na-z\n"
i get these matchs:
sol
perro
idea
caballo
tres
cuatro
cinco
pesos
dollar
ccc
coton
chien
please how i change my regex ???? if i want 8cuatro\n 3pesos\n $dollar$\n and chien#\n not matched....thanks lot of
bye
If you don't want to match the leading space, you can omit that from the pattern, and you can also omit the capture group if you want matches only.
You can assert a whitspace boundary to the left, and at the right side a word boundary followed by asserting not # to the right.
(?<!\S)[a-zA-Z]{3,}\b(?!#)
See a regex demo
In Java:
String regex = "(?<!\\S)[a-zA-Z]{3,}\\b(?!#)";
String string = "sol perro idea \n"
+ "caballo\n"
+ "do7\n"
+ "tres\n"
+ " tr_es\n"
+ "8cuatro\n"
+ "cinco.\n"
+ "3pesos\n"
+ "$dollar$\n"
+ "ccc\n"
+ "coton\n"
+ "H7T\n"
+ " chien#\n"
+ "a-z\n";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Output
sol
perro
idea
caballo
tres
cinco
ccc
coton

Matching regular expressions

I have a regular expression, it's basically to update log4j syntax to log4j2 syntax, removing the string replacement. The regular expression is as follows
(?:^\(\s*|\s*\+\s*|,\s*)(?:[\w\(\)\.\d+]*|\([\w\(\)\.\d+]*\s*(?:\+|-)\s*[\w\(\)\.\d+]*\))(?:\s\+\s*|\s*\);)
This will successfully match the variables in the following strings
("Unable to retrieve things associated with this='" + thingId + "' in " + (endTime - startTime) + " ms");
("Persisting " + things.size() + " new or updated thing(s)");
("Count in use for thing=" + secondThingId + " is " + countInUse);
("Unable to check thing state '" + otherThingId + "' using '" + address + "'", e);
But not '+ thingCollection.get(0).getMyId()' in
("Exception occured while updating thingId="+ thingCollection.get(0).getMyId(), e);
I am getting better with regular expressions, but this one has me a bit stumped. Thanks!
For some reason, when some people are writing a regex pattern, they forget that the whole of the Perl language is still available
I would just delete all the strings and find the remaining substrings that look like variable names
use strict;
use warnings 'all';
use feature qw/ say fc /;
use List::Util 'uniq';
my #variables;
while ( <DATA> ) {
s/"[^"]*"//g;
push #variables, /\b[a-z]\w*/ig;
}
say for sort { fc $a cmp fc $b } uniq #variables;
__DATA__
("Unable to retrieve things associated with this='" + thingId + "' in " + (endTime - startTime) + " ms");
("Persisting " + things.size() + " new or updated thing(s)");
("Count in use for thing=" + secondThingId + " is " + countInUse);
("Unable to check thing state '" + otherThingId + "' using '" + address + "'", e);
("Exception occured while updating thingId="+ thingCollection.get(0).getMyId(), e);
output
address
countInUse
e
endTime
get
getMyId
otherThingId
secondThingId
size
startTime
thingCollection
thingId
things
You should be able to simplify your regex to match things in between '+' signs.
(?:\+)([^"]*?)(?:[\+,])
Working Example
(Note the ? after the * this makes the * lazy so it matches as little as possible to catch all occurrences)
If you want just the variable you could access the first capture group from that expression or ignore the capture group to get the full match.
Updated Version (?:\+)([^"]*?)(?:[\+,])|\s([^"+]*?)\);Working Example
Note with the new version that the variable might get placed into capture group 2 instead of 1
You might be able to pare it down to this (?:^\(\s*|\s*\+\s*|,\s*)(?:[\w().\s+]+|\([\w().\s+-]*\))(?:(?=,)|\s*\+\s*|\s*\);)
101 regex
It consolidates some constructs.
To fix the immediate problem, I added a comma in some classes.
A note that this kind of regex is fraught with problematic type of flow.
(?:
^ \( \s*
| \s* \+ \s*
| , \s*
)
(?:
[\w().\s+]+
| \( [\w().\s+-]* \)
)
(?:
(?= , )
| \s* \+ \s*
| \s* \);
)

Divide cell in excel by 3 different string patterns using vba

I have a list with a row in excel which has three different types (pattern) of string:
ABCD, CBFG, EXAL
For the first type it's for example: ABCD
So it will always be a combination of letters and no numbers or anything else.
ABCD (HOLA), CBFG (HOLA), EXAN (HOLA)
For the second type it's for example: ABCD (HOLA)
So it will be always a combination of letters and a combination of letters in brackets.
ABCD (HOLA), SOLE, CBFG (HOLA), SUPRA, EXAN (HOLA), ITAN
For the third type it's for example: ABCD (HOLA), SOLE
So it will be always a combination of letters and a combination of letters in brackets and a comma followed by another combination of letters
All these three types are listed with the following pattern.
Let's say we have: ABCD and ABCD (SOLE) and ABCD (HOLA), SOLE
Then my cell will look as follows:
ABCD, ABCD (HOLA), ABCD (HOLA), SOLE
Now I have the type 3 which also includes a comma. You can say there are two different commas here. One is used to divide each entry and the other one
is actually a part of one entry.
What I am trying to do now is to get these three types and paste them into another cell.
I don't know how to go about this. If someone can give me an advice on how to start that would be great.
Here is an example how I want my cell to divide the information:
In B2 is the information I have. The range H1 to J4 is what I want to have
Soshiribo,
I think that will would work for you. You would obviously want to rename and define all of the variables and change the limits on the loop but this should work.
Dim string2() As String
For I = 1 To 3
Erase string2()
string1 = ActiveSheet.Cells(I + 1, 2)
string2() = Split(string1, ",")
x = UBound(string2) - LBound(string2) + 1
Select Case x:
Case Is = 5
String3 = string2(0) + "," + string2(1)
String4 = string2(2)
String5 = string2(3) + "," + string2(4)
ActiveSheet.Cells(I + 1, 4) = String3
ActiveSheet.Cells(I + 1, 5) = String4
ActiveSheet.Cells(I + 1, 6) = String5
Case Is = 6
String3 = string2(0) + "," + string2(1)
String4 = string2(2) + "," + string2(3)
String5 = string2(4) + "," + string2(5)
ActiveSheet.Cells(I + 1, 4) = String3
ActiveSheet.Cells(I + 1, 5) = String4
ActiveSheet.Cells(I + 1, 6) = String5
End Select
Next I
If you're looking for a regex, here's an idea:
(\w+)( \(HOLA\))?,? ?(SOLE|SUPRA|ITAN)?
I don't fully understand all the requirements, so this is really just a starting point.

How to convert C style cast to C++ style cast in vim

I got hand over some legacy code and first I want to change
(int)a + b;
into
static_cast<int>(a) + b;
There are a lot of them and doing them manually is very time consuming. Is there a way to use vim to make this happen?
I tried something like
:%s/\(int\).* /static_cast<int>(\2)/g
but it doesn't work. Please advice.
Try this:
:%s/(\(.*\))\([^ ]*\)/static_cast<\1>(\2)/g
This regex, as per your question, assumes that there will be a space after the variable name:
Example:
For following test data:
(int)a + b
(float)x * y
(int)z+m
result will be
static_cast<int>(a) + b
static_cast<float>(x) * y
static_cast<int>(z+m)
Explaining the regex
(\(.*\)) - Match whatever is inside () and capture it
\([^ ]*\) - followed by anything which is not a space and capture it
You can use this:
%s/(int)\(a\)/static_cast<int>(\1)/g
This is assuming variable name always is a. If it is not then you can replace a with [a-z].
I have several mappings for this task in lh-cpp.
In that case, it'll be ,,sc, or ,,rc, or ,,dc. (here, , is actually my <localleader>).
It's actually implemented as:
function! s:ConvertToCPPCast(cast_type)
" Extract text to convert
let save_a = #a
normal! gv"ay
" Strip the possible brackets around the expression
let expr = matchstr(#a, '^(.\{-})\zs.*$')
let expr = substitute(expr, '^(\(.*\))$', '\1', '')
"
" Build the C++-casting from the C casting
let new_cast = substitute(#a, '(\(.\{-}\)).*',
\ a:cast_type.'<\1>('.escape(expr, '\&').')', '')
" Do the replacement
exe "normal! gvs".new_cast."\<esc>"
let #a = save_a
endfunction
vnoremap <buffer> <LocalLeader><LocalLeader>dc
\ <c-\><c-n>:'<,'>call <sid>ConvertToCPPCast('dynamic_cast')<cr>
nmap <buffer> <LocalLeader><LocalLeader>dc viw<LocalLeader><LocalLeader>dc
...

How do I capture all occurences in a string in Vim?

I want to capture all certain occurrences in a string in Vimscript.
example:
let my_calculation = '200/3 + 23 + 100.5/3 -2 + 4*(200/2)'
How can I capture all numbers (including dots if there are) before and after the '/'? in 2 different variables:
- output before_slash: 200100.5200
- output after slash 332
How can I replace them if a condition occurs?
p.e. if after a single '/' there is no '.' add '.0' after this number
I tried to use matchstring and regex but after trying and trying I couldn't resolve it.
A useful feature that can be taken advantage of in this case is substitution
with an expression (see :help sub-replace-\=).
let [a; b] = [[]]
call substitute(s, '\(\d*\.\?\d\+\)/\(\d*\.\?\d\+\)\zs',
\ '\=add(a,submatch(1))[1:0]+add(b,submatch(2))[1:0]', 'g')
To answer the second part of the question:
let my_calculation = '200/3 + 23 + 100.5/3 -2 + 4*(200/2)'
echo substitute(my_calculation, '\(\/[0-9]\+\)\([^0-9.]\|$\)', '\1.0\2', 'g')
The above outputs:
200/3.0 + 23 + 100.5/3.0 -2 + 4*(200/2.0)
Give this a try:
function! GetNumbers(string)
let pairs = filter(split(a:string, '[^0-9/.]\+'), 'v:val =~ "/"')
let den = join(map(copy(pairs), 'matchstr(v:val, ''/\zs\d\+\(\.\d\+\)\?'')'), '')
let num = join(map(pairs, 'matchstr(v:val, ''\d\+\(\.\d\+\)\?\ze/'')'), '')
return [num, den]
endfunction
let my_calculation = '200/3 + 23 + 100.5/3 -2 + 4*(200/2)'
let [a,b] = GetNumbers(my_calculation)
echo a
echo b