How to find word in string using regular expression - regex

How to find word in string using regular expression regardless of case sensitivity ?
Exmaple:
A1234_front123 in this need to find _front word
a1235Front1234yy in this I need to find front
I need to design generic regular expression which can find any kind of words in given string

If front could be another word, this should try with such combination:
(?<=[0-9])[A-Za-z_]+(?=[0-9_])
DEMO
it will work without case insensitive mode i

You must use i modifier.
/front/i
Online example: https://regex101.com/r/hQ9hH6/1

Related

regular expression match case sensitive off

I have a regular expression that finds two words in a line.
The problem is that it is case sensitive. I need to edit it so that it matches both the case.
reqular expression
^(.*?(\bPain\b).*?(\bfever\b)[^$]*)$
You can use RegexOptions.Ignorecase to set case insensitive matching mode. This way you make the entire pattern case insensitive. The same effect can be achieved with (?i) inline option at the beginning of the pattern:
(?i)^(.*?(\bPain\b).*?(\bfever\b)[^$]*)$
You can use the inline flag to only set case insensitive mode to part of a pattern:
^(.*?(\b(?i:Pain)\b).*?(\b(?i:fever)\b)[^$]*)$
Or you can just match "pain" or "Pain" with
^(.*?(\b(?i:P)ain\b).*?(\bfever\b)[^$]*)$
Another alternative is using character classes [Pp], etc.
Note that you do not have to set a capturing group round the whole pattern, you will have access to it via rx.Match(str).Groups(0).Value.
^.*?(\b[pP]ain\b).*?(\b[Ff]ever\b)[^$]*$
You can usually set a flag for that, depending on your language, or you can mess up your regex into a more ugly looking one using multiple character classes. [pP][aA][iI][nN] is essentially the word "pain" without it being case sensitive at all.
Well, if you're using VB.net, you can tell the regex object to ignore the case sensitivity when you create it
'Defines the pattern
Dim MyPattern As String = "BlaBla"
'Create a new instance of the regex class with the above pattern
'and the option to ignore the casing
Dim Regex As New Regex(MyPattern, RegexOptions.IgnoreCase)

Regular expression to replace shortest match

my string is like this
sfdfdsfdsfstart112matlab2336endgfdgdfgkknfkgstart558899enddfdsfd
how can we replace part of a string such a way that the result will be
sfdfdsfdsfgfdgdfgkknfkgdfdsfd
i.e bolded content need to be removed.
You need to use non-greedy matching:
start.*?end
Use replacement function with this regex /start.+?end/g which will match the bold parts of your string. The g part of the regex means globally, and might need to be implemented differently depending on the language you use.
The key here is to use ? which turns on un-greedy matching. That means the match consumes the minimum amount of characters rather than the maximum, so will match from the start to the next rather than the last end
start[1-9]+end
if you need to have numbers between words

Regex match first characters of string

I am trying to create a regex that will match the first 3 characters of a string,
If I have a string ABCFFFF I want to verify that the first 3 characters are ABC.
It's pretty straightforward, the pattern would be ^ABC
As others may point out, using regular expressions for such a task is an overkill. Any programming language with regex support can do it better with simple string manipulations.
Just simple regex will work:
/^ABC/
But is it a good use case for using regex, I am not sure. Consider using substring in whatever language/platform you're using.
"^ABC" should work. '^' matches the start in most regex implementations.

is it the right reqular expression

i have following regular expression but it's not working properly it takes only three values after # sign but i want it to be any number length
"/^[a-zA-Z0-9_\.\-]+\#([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/"
this#thi This is validated
this#this It is not validating this expression
Can you please tell me what's the problem with the expression...
Thanks
If you want your regex to match "any number length" then why are you using {2,4}?
I think a better example of the strings you're trying to match might give others a better idea of what you want, because based on your regex it is a bit confusing what you're looking for.
Try this:
^[a-zA-Z0-9_.-]+#([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$
The main problem is that you didn't escape the dot: \.. In regular expression the dot matches everything (mostly), making your regex quite liberal.

Extract and use a part of string with a regex in GVIM

I've got a string:
doCall(valA, val.valB);
Using a regex in GVIM I would like to change this to:
valA = doCall(valA, val.valB);
How would I go about doing this? I use %s for basic regex search and replace in GVIM, but this a bit different from my normal usages.
Thanks
You can use this:
%s/\vdoCall\(<(\w*)>,/\1 = doCall(\1,/
\v enables “more magic” in regular expressions – not strictly necessary here but I usually use it to make the expressions simpler. <…> matches word boundaries and the in-between part matches the first parameter and puts it in the first capture group. The replacement uses \1 to access that capture group and insert into the right two places.