In .htaccess file, \w matches a word, i.e., numbers or letters. But it does not matches special characters like -. So, what escape character matches these characters ?
To match - with word character \w you will need to use character class:
[\w-]
Related
I would like to mark in Notepad++ the sql scripts in a text log. The sql files have this format in the text:
AAAAAAAA.BBBBBBBBBBB.sql
So what I execute is this sentence in search menu:
\w*.sql
As I should get BBBBBBBBBBB.sql. The point is that in some script names there are dashes (-), and when that happens I dont get the whole name, but just the end after the last dash.
For example, in:
AAAAAAAA.BBBBB-CCCCCCC.sql
I would like to get BBBBB-CCCCCCC.sql, but I just get CCCCCCC.sql
Is there any possible formula to get them?
If the match can not start and end with a hyphen:
\w+(?:-\w+)*\.sql
\w+ Match 1+ word characters
(?:-\w+)* Optionally match - and 1+ word characters
\.sql Match .sql
See a regex demo.
Note that in your pattern the \w* can also match 0 occurrences and that the . can match any character if it is not escaped.
Another option could be using a character class to match either - or a word character, but this would also allow to mix and match like --a--.sql
[\w-]+\.sql
See another regex demo.
iIf a dash is in the string "grep -w" is not unique. How can I solve this?
Example:
File1:
football01 football01test
# grep -iw ^football01
football01
File2:
football01 football01-test
# grep -iw ^football01
football01
football01-test
This is the expected and documented behaviour:
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the
matching substring must either be at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end of the line or followed
by a non-word constituent character. Word-constituent characters are letters, digits,
and the underscore.
If you add a dash, it terminates your first word as a dash is a "non-word constituent character". If you write the words together, then a word-regexp grep will treat it as one word and not match it.
What exactly it is that you want to do?
If you only want to know if your line is football01 and nothing else, you can do it as
grep -i "^football01$"
If you want to achieve something else, could you please explain what it is.
The -w switch is for word regex. In file1, football01test is a word and in file2 football01 and test are two words separated by a hyphen.
man grep says this for -w
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be at the
beginning of the line, or preceded by a non-word constituent
character. Similarly, it must be either at the end of the
line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.
Since football01 doesn't match football01test as a whole word, you aren't getting that info from grep.
If you were to do grep -i ^football01 file1.txt, you will get both lines.
How do I put several characters after the first letter and the last letter in the whole text by use of Vim?
E.g. I need to put {{c1:: after the first letter and }} after the last letter. Also, I want to ignore two-letter words.
You mean in every word? Try this:
:%s/\<\(\w\)\(\w\w\+\)\>/\1{{c1::\2}}/g
That will replace every first character in a word with the first character followed by {{c1:: and add }} at the end of it. Words shorter than three characters are ignored.
If your words contain more than just [a-zA-Z0-9], then replace \w by a more appropriate character class.
I need to do a case sensitive search in gvim.
The pattern that I want to search looks something like:
For ex:
tABCD_EFGH_IJKL
Here the first alphabet 't' is a permanent character, after which the word starts with a capital letter (any alphabet).
Please help.
Try this regex:
t[A-Z]{4}_[A-Z]{4}_[A-Z]{4}
To understand it, let's break it down.
t "t"
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
_ Underscore
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
_ Underscore
[A-Z] Any uppercase letter (from A to Z)...
{4} ...4 times
To search with a regex in vim, hit / (forward slash) in Insert mode, type the pattern, then press Enter.
I've used string replacement in Perl a couple of times and have particular substrings and replace them with something else.
I'm curious if there is a trick to only keep certain characters, specifically I want to remove any characters from the string that are not a-z, A-Z or 0-9.
E.g., a b c !##$%^&*()_~+=[]{}\|;':",./<>? 123 would just be abc123.
Using regex,
s/[^a-zA-Z0-9]//g;
using translation,
tr/a-zA-Z0-9//dc;