FIND AND REPLACE TEXT IN MULTIPLE FILES in LINUX

To replace a string in multiple files you can use:

RUN THIS:

First find the text inside the directory recursively:

grep -rnw matchstring somedir/

or

grep -rnw 'directory' -e 'matchstring' 

List the filename with “text that you are finding’;

grep -rl matchstring somedir/
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

EXAMPLE OF ABOVE COMMAND CHANGE THE WORD “windows” to “linux” on current direcotry recursively.

grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'

REMEMBER THIS!! CHANGING LINKS OR AVOIDING SPECIAL CHARACTERS USE “@”

To replace a path within files (avoiding escape characters) you may use the following command:

sed -i 's@old_path@new_path@g'

I WILL GIVE YOU AN EXAMPLE I QUERY ALL http://localhost in my /usr/local/var directory recursively this command list filename and the matchingtext inside your file.

grep -rnw usr/local/var -e http://localhost

TO LIST ALL THE FILENAME WITH MATCHING CONTENT OF THE FILE THIS USE THE COMMAND BELOW

grep -rl usr/local/var -e http://localhost

TO FIND AND REPLACE ALL http://localhost to https://localhost RUN THE COMMAND BELOW REMEMBER THE ONE I TOLD YOU ABOVE THE “@”.

grep -rl 'http://localhost' /usr/local/var | xargs sed -i 's@http://localhost@https://localhost@g'

ANOTHER EXAMPLE change ‘define(“DB_HOST”, “localhost”);’ TO ‘define(“DB_HOST”, “192.168.1.100”);’ FOCUS THE POSITION OF SINGLE QUOTE(‘).

SEARCH THE FILE CONTENT THE MATCH STRING you can use -rli and rnw

grep -rnw 'define("DB_HOST", "localhost");' /var/www

NOW APPLY THE COMMAND TO FIND AND REPLACE RECURSIVELY you can use -rl or -rnw I HAVE EXAMPLE ABOVE WHAT IS THE EFFECTS OF THAT COMMAND FOCUS IN SINGLE QUOTE (‘) POSITION BEFORE RUNNING THE COMMAND.

Posted in LINUX | Leave a comment

CROSS TAB QUERY IN MYSQL

MySQL Multi-Aggregated Rows in Crosstab Queries

MySQL’s crosstabs contain aggregate functions on two or more fields, presented in a tabular format. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to multiple fields on the same (row or column) axis. Rob Gravelle shows you how to apply two different functions to the same field in order to create grouping levels in the row axis.

Today’s topic of discussion is crosstabs, which contain multiple aggregate functions in the row axis of a tabular resultset. Recall from the the All About the Crosstab Query article that an aggregate function is one that summarizes a group of related data in some way. Examples of aggregate functions include COUNT, SUM, AVG, MIN, and MAX. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to two or more fields. Today we’ll create a query that applies two different functions to the same field in order to create grouping levels in the row axis.

Recap of Crosstab Basics

In the Tips for Simplifying Crosstab Query Statements article, we took the complex SQL statement that we created in the All About the Crosstab Query article and simplified it to a more streamlined version:

SELECT CASE WHEN Month_Num IS NULL 
            THEN 'TOTAL' 
            ELSE Month 
       END      AS 'Month',
  REGION_1 AS 'REGION 1',  
  REGION_2 AS 'REGION 2',
  REGION_3 AS 'REGION 3',
  REGION_4 AS 'REGION 4',
  REGION_5 AS 'REGION 5',       
  TOTAL
FROM  (SELECT MONTH(CREATION_DATE)                          AS Month_Num,
     MONTHNAME(CREATION_DATE)                               AS Month,
     COUNT(CASE WHEN REGION_CODE ='01' THEN FEE_NUMBER END) AS REGION_1,
     COUNT(CASE WHEN REGION_CODE ='02' THEN FEE_NUMBER END) AS REGION_2,
     COUNT(CASE WHEN REGION_CODE ='03' THEN FEE_NUMBER END) AS REGION_3,
     COUNT(CASE WHEN REGION_CODE ='04' THEN FEE_NUMBER END) AS REGION_4,
     COUNT(CASE WHEN REGION_CODE ='05' THEN FEE_NUMBER END) AS REGION_5,
     COUNT(*)                                               AS TOTAL 
  FROM   TA_CASES
  WHERE  YEAR(CREATION_DATE)=1998
  GROUP BY Month_Num WITH ROLLUP) AS CA;

The subquery fetched all of the fields that we needed, plus the month number, for sorting.  We then selected from it by placing the code after the FROM of a second query. Performing a query in a two pass process in this way is called Pre (or Partial)-Aggregation. The first pass creates a derived table or resultset that performs most of the aggregation work, while the next pass does some formatting and any additional calculations that may be required.  The neccessitating of pre-aggregation here was the result of two factors: The ROLLUP GROUP BY modifier inserted a Null row into the resultset, which was difficult to replace with the “TOTAL” row header because of its late evaluation in the query process.  A second challenge was presented by the grouping on the output of date functions because grouping on the MONTHNAME() sorted the rows in alphabetical order, rather than chronological.

The above query produced the following desired crosstab, including chronological row sorting and totals:

Month REGION 1 REGION 2 REGION 3 REGION 4 REGION 5 TOTAL
April 13 33 76 2 47 171
May 17 55 209 1 143 425
June 8 63 221 1 127 420
July 13 104 240 6 123 486
August 18 121 274 9 111 533
September 25 160 239 2 88 514
October 9 88 295 2 127 521
November 2 86 292 2 120 502
December 1 128 232 6 155 522
TOTAL 106 838 2078 31 1041 4094

Multi-Aggregate Pivots

This query was not terribly complex as it only hit one table and pivoted between two fields: the Month and Region.  That’s called a Single Pivot.  If we wanted to breakdown the time periods further into other time periods, we would now be looking at a two-to-one pivot (eg: months and weeks per region).  On the column side, regions could likewise be broken down into specific cities, giving us a one-to-two pivot.  Adding the new fields to both the columns and rows would produce a true many-to-many multi-aggregate pivot.  The following chart illustrates the inherent complexety of multi-aggregate pivots:

A B C1 C2 Total column header 1
D1 D2 Total D1 D2 Total column header 2
E1 E2 Total E1 E2 Total E1 E2 Total E1 E2 Total column header 3
A1 B1
B2
Total row sub total
A2 B1
B2
Total row sub total
Total row total
row
header
1
row
header
2
col
sub
total
col
sub
total
col
sub
total
col
sub
total
col
sub
total
col
sub
total
col
total

 

Reporting on Two Row Fields

We’re going to create a Multi-Aggregate row crosstab by adding the Year to the row data, making it the new A row in the above diagram, thus displacing the months to the B field. That will allow us to report on multiple years, by calling the YEAR() function on the CREATION_DATE, much like we did for displaying the months. We’ll insert it as the first field in the SELECT list (new code appears in Red):

...
FROM  (SELECT YEAR(CREATION_DATE)              AS Year,
              MONTH(CREATION_DATE)             AS Month_Num,
              MONTHNAME(CREATION_DATE)         AS Month,
		 ...

We can also easily include totals for each year by adding it to the GROUP BY clause:

       GROUP BY Year, Month_Num WITH ROLLUP) AS CA;

In addition to needing a column heading for the years, we also need to alter the months CASE statement, because rows which display the yearly totals will contain a NULL Month_num value:

SELECT CASE WHEN Year IS NULL
            THEN 'GRAND TOTAL' 
            ELSE Year 
       END      AS 'Year',
       CASE WHEN Month_Num IS NULL     
            THEN CASE WHEN Year IS NULL
                      THEN ''                                         
                      ELSE CONCAT(Year, ' TOTAL')                      
                 END
            ELSE Month 
       END      AS 'Month',  
	 ...

Here then is the full SQL statement to include the Year rows.

SELECT CASE WHEN Year IS NULL
            THEN 'GRAND TOTAL' 
            ELSE Year 
       END      AS 'Year',
       CASE WHEN Month_Num IS NULL     
            THEN CASE WHEN Year IS NULL
                      THEN ''                                         
                      ELSE CONCAT(Year, ' TOTAL')                      
                 END
            ELSE Month 
       END      AS 'Month',  
       REGION_1 AS 'REGION 1',  
       REGION_2 AS 'REGION 2',
       REGION_3 AS 'REGION 3',
       REGION_4 AS 'REGION 4',
       REGION_5 AS 'REGION 5',       
       TOTAL
FROM  (SELECT YEAR(CREATION_DATE)                                    AS Year,
              MONTH(CREATION_DATE)                                   AS Month_Num,
              CONVERT(MONTHNAME(CREATION_DATE) USING latin1)         AS Month,
              COUNT(CASE WHEN REGION_CODE ='01' THEN FEE_NUMBER END) AS REGION_1,
              COUNT(CASE WHEN REGION_CODE ='02' THEN FEE_NUMBER END) AS REGION_2,
              COUNT(CASE WHEN REGION_CODE ='03' THEN FEE_NUMBER END) AS REGION_3,
              COUNT(CASE WHEN REGION_CODE ='04' THEN FEE_NUMBER END) AS REGION_4,
              COUNT(CASE WHEN REGION_CODE ='05' THEN FEE_NUMBER END) AS REGION_5,
              COUNT(*)                                               AS TOTAL 
       FROM   TA_CASES
       WHERE  YEAR(CREATION_DATE)>2003
       GROUP BY Year, Month_Num WITH ROLLUP) AS CA;

The revised SQL code produces the following result, which includes the YEAR headers in the first column, and the TOTAL summary row for each year:

Year Month REGION 1 REGION 2 REGION 3 REGION 4 REGION 5 TOTAL
2004 January 8 41 156 1 42 248
2004 February 1 38 140 0 29 212
2004 March 0 44 115 0 50 209
2004 April 4 45 119 0 42 210
2004 May 1 57 151 0 84 294
2004 June 2 63 142 0 48 259
2004 July 6 47 110 0 33 199
2004 August 10 38 150 1 53 256
2004 September 3 45 146 7 34 235
2004 October 2 55 112 0 34 204
2004 November 4 36 110 1 43 195
2004 December 1 30 165 0 31 227
2004 2004 TOTAL 42 539 1616 10 523 2748
2005 January 3 34 107 2 34 182
2005 February 0 24 103 0 15 144
2005 March 1 30 101 0 24 159
2005 April 3 27 149 0 27 212
2005 May 1 36 110 0 27 177
2005 June 3 52 113 0 28 196
2005 July 1 38 131 3 22 195
2005 August 4 51 146 1 36 238
2005 September 9 52 149 0 49 259
2005 October 10 29 100 0 47 186
2005 November 11 11 162 0 18 204
2005 December 4 66 142 0 19 231
2005 2005 TOTAL 50 450 1513 6 346 2383
2006 January 2 68 132 0 30 235
2006 February 1 43 94 0 24 162
2006 March 3 30 134 0 29 196
2006 April 1 47 129 0 21 199
2006 May 11 52 124 0 31 220
2006 June 9 49 126 0 30 214
2006 July 4 43 125 0 42 217
2006 August 3 50 132 0 51 241
2006 September 6 56 149 0 45 262
2006 October 5 31 121 2 25 184
2006 November 3 42 146 3 63 258
2006 December 0 47 115 0 23 194
2006 2006 TOTAL 48 558 1527 5 414 2582
2007 January 2 37 152 1 35 229
2007 February 1 35 86 2 31 156
2007 March 5 78 132 0 51 268
2007 April 2 41 125 1 41 211
2007 May 0 50 122 0 33 206
2007 June 1 63 107 0 38 209
2007 July 5 41 65 1 31 150
2007 August 0 63 110 2 43 219
2007 September 2 35 134 0 55 227
2007 October 2 39 120 2 52 225
2007 November 10 22 141 0 36 215
2007 December 51 26 60 0 17 156
2007 2007 TOTAL 81 530 1354 9 463 2471
2008 January 1 52 154 1 49 277
2008 February 0 12 84 1 34 136
2008 March 0 30 85 1 27 148
2008 April 3 22 73 0 40 168
2008 May 0 48 90 2 29 170
2008 June 10 63 122 0 24 225
2008 July 19 38 148 3 28 238
2008 August 54 50 105 0 20 230
2008 September 42 34 143 2 44 268
2008 October 73 41 112 1 26 253
2008 November 11 15 101 0 20 152
2008 December 285 62 160 2 29 541
2008 2008 TOTAL 498 467 1377 13 370 2806
2009 January 686 27 123 3 42 882
2009 February 2 9 87 0 22 120
2009 March 4 19 106 0 25 154
2009 April 0 10 95 0 12 117
2009 May 4 31 93 0 21 151
2009 June 63 71 94 0 21 251
2009 July 1 42 92 6 21 165
2009 August 3 53 116 3 19 195
2009 September 1 12 25 0 11 49
2009 December 0 0 1 0 10 17
2009 2009 TOTAL 764 274 832 12 204 2101
GRAND TOTAL   1483 2818 8219 55 2320 15091

Adding row fields is not all that difficult because queries naturally group data by rows. Therefore, all that is required is to add the new row to the SELECT and GROUP BY field lists and include a column for the row headers, as we did above. In the next installment, we will take on the more imposing challenge of adding a new column. It’s not as straighforward as might initially appear.

SOURCE: http://www.databasejournal.com/features/mysql/article.php/3865811/MySQL-Multi-Aggregated-Rows-in-Crosstab-Queries.htm

crosstab

PICTURE SOURCE: http://sqlfiddle.com/#!9/ca16b/76

 

Posted in MYSQL | Leave a comment

HOW TO CONNECT JAVA TO MYSQL DATABASE?

  1. DOWNLOAD AND INSTALL MYSQL SERVER
  2. DOWNLOAD THE JDBC Driver extract the ZIP file and put the containing JAR file in the classpath or in current directory of your code.
  3. MAKE SURE YOU HAVE YOUR MYSQL USERS TABLE IN YOUR [DATABASE] WITH USERNAME COLUMN

4. CREATE A FILENAME MySQLConnectExample.java

5. PUT THIS CODE INSIDE THE FILE. (TAKENOTE: CASE SENSITIVE)

import java.sql.*;
import java.util.Properties;

public class MySQLConnectExample
{
public static void main(String[] args)
{

Connection conn = null;
Statement stmt = null;
try {
String url1 = “jdbc:mysql://localhost:3306/dbname”; //your database name
String user = “root”; //login user
String password = “password”; //user db password

conn = DriverManager.getConnection(url1, user, password);
if (conn != null)
System.out.println(“Connected to the database test1”);

System.out.println(“Creating a statement..”);
stmt = conn.createStatement();
String sql;
sql = “SELECT username FROM users;”;
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String value1 = rs.getString(“username”);
System.out.println(“users: ” + value1);
}

} catch (SQLException ex) {
System.out.println(“An error occurred. Maybe user/password is invalid”);
ex.printStackTrace();
}
}
}

SEE THE IMAGE BELOW TO THE FOLDER CONTENTS ON HOW TO COMPILE AND RUN THE PROGRAM.

6. COMPILE: javac MySQLConnectionExample.java

7. RUN: java -cp mysql-connector-java-5.1.37-bin.jar;. MySQLConnectExample
connect_java_to_mysql

Posted in JAVA | Leave a comment

JAVASCRIPT – ABOUT USING REGEX OR REGULAR EXPRESSIONS

About Using Regular Expressions

Content Manager includes a powerful string-matching tool called regular expressions, often abbreviated as regex. With regular expressions, you can create filters that can match patterns of text rather than only single words or phrases.

Regular expressions are a standard tool in many systems and scripting languages. Regular expressions can be simple or highly complex. This document provides information about how to use regular expressions with Content Manager, and can help you create simple expressions. You can find more detailed information—including tutorials and examples—on many web sites, such as the following:

Uses for Regular Expressions

Using regular expressions, you can create content filters that can find:

*
Text patterns

Use this option to scan messages for patterns of letters, numbers, or a combination of both. For example, you can create regular expressions that match phone numbers, addresses, employee numbers, and account numbers. Or, you can create one regular expression that can find many different variations of a word, such as viagra, vi@gra, v1agr@, and so on.

*
Lists of words

Use this option to scan messages for specific categories of words, such as profanity, financial terms, and legal terms.

*
Complete words

Use this option to create more specific filters. For example, you can create a regular expression that matches the word foot but not football. In this case, a regular expression can help to reduce the number of legitimate messages that the filter captures.

*
Text with variable characters

Use this option to scan messages for patterns that contain specific text along with text that varies. For example, you can create a single regular expression that matches a URL in the pattern http://www.[variable].com, such as http://www.abc1.com, http://www.abc2.com, and http://www.abc3.com.

Regular Expressions Syntax

To create a regular expression, you must use specific syntax—that is, special characters and construction rules. For example, the following is a simple regular expression that matches any 10-digit telephone number, in the pattern nnn-nnn-nnnn:

\d{3}-\d{3}-\d{4}

Note: Content Manager supports only POSIX ERE syntax and equivalent shorthand characters. For more information, see Content Manager Support for Regular Expressions.

The following table describes some of the most common special characters for use in regular expressions. These characters are categorized as follows:

 
Character
Description

Anchors

^

(caret) Matches the start of the line or string of text that the regular expression is searching. For example, a content rule with a location Subject line and the following regular expression:

^abc

captures any email message that has a subject line beginning with the letters abc.

$

(dollar) Matches the end of the line or string of text that the regular expression is searching. For example, a content rule with a location Subject line and the following regular expression:

xyz$

captures any email message that has a subject line ending with the letters xyz.

Metacharacters

.

(dot) Matches any single character, except a new line.

|

(pipe) Indicates alternation—that is, an “or.” For example:

cat|dog matches the word cat or dog

\

Indicates that the next character is a literal rather than a special character. For example:

\. matches a literal period, rather than any character (dot character)

Character Classes

[…]

Matches any character from a set of characters. Separate the first and last character in a set with a dash. For example:

[123] matches the digit 1, 2, or 3

[a-f] matches any letter from a to f

Note: Regular expressions in Content Manager are not case sensitive. Therefore, using this formatting to specify a character set in lowercase letters also includes the equivalent uppercase letters.

[^…]

Matches any character not in the set of characters. For example:

[^a-f] matches any character that’s not a letter from a to f

Note: Regular expressions in Content Manager are not case sensitive. Therefore, using this formatting to specify a character set in lowercase letters also excludes the equivalent uppercase letters.

[:alnum:]

Matches alphanumeric characters (letters or digits):

a-z, A-Z, or 0-9

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:almun:]].

[:alpha:]

Matches alphabetic characters (letters):

a-z or A-Z

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:alpha:]].

[:digit:]

Matches digits:

0-9

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:digit:]].

[:graph:]

Matches visible characters only—that is, any characters except spaces, control characters, and so on.

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:graph:]].

[:punct:]

Matches punctuation characters and symbols:

! ” # $ % & ‘ ( ) * + , \ -. / : ; < = > ? @ [ ] ^ _ ` { | }

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:punct:]].

[:print:]

Matches visible characters and spaces.

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:print:]].

[:space:]

Matches all whitespace characters, including spaces, tabs, and line breaks.

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:space:]].

[:word:]

Matches any word character—that is, any letter, digit, or underscore:

a-z, A-Z, 0-9, or _

Note: This character class must be surrounded with another set of square brackets when you use it in a regular expression, for example: [[:word:]].

Shorthand Character Classes

\w

Matches any word character—that is, any letter, digit, or underscore:

a-z, A-Z, 0-9, or _

Equivalent to [:word:]

\W

Matches any non-word character—that is, any character that’s not a letter, digit, or underscore.

Equivalent to [^[:word:]]

\s

Matches any whitespace character. For example, use this character to specify a space between words in a phrase:

stock\stips matches the phrase stock tips

Equivalent to [:space:]

\S

Matches any character that’s not a whitespace.

Equivalent to [^[:space:]]

\d

Matches any digit from 0-9.

Equivalent to [:digit:]

\D

Matches any character that’s not a digit from 0-9.

Equivalent to [^[:digit:]]

Group

(…)

Groups parts of an expression. Use grouping to apply a quantifier to a group or to match a character class before or after a group. For example, in the following expression:

\W(dog|cat|mouse)\W

the \W character class applies before and after each word in the group. The expression would match things like dog+cat or *dog*.

Quantifiers

{n}

Match the preceding expression exactly n times. For example:

[a-c]{2} matches any letter from a to c only if two letters occur in a row. Thus, the expression would match ab and ac but not abc or aabbc.

{n,m}

Match the preceding expression a minimum of n times and a maximum of m times. For example:

[a-c]{2,4} matches any letter from a to c only if the letters occur a minimum of 2 times and a maximum of 4 times in a row. Thus, the expression would match ab and abcbut not aabbc.

?

Indicates that the preceding character or expression can match 0 or 1 times. Equivalent to the range {0,1}. For example, the following regular expression:

colou?r

matches either colour or color, because the ? makes the letter u optional.

Content Manager Support for Regular Expressions

Content Manager provides robust support for regular expressions. However, because Content Manager filters large volumes of email messages, it does not support all regular-expressions syntax standards or features.

Regex Syntax Support

There are many variations of regular-expressions syntax and features. Content Manager supports only the POSIX Extended Regular Expressions (ERE) standard and shorthand notation for some character classes. It doesn’t support other standards, such as Perl or .NET regular expressions.

Case Sensitivity in Regular Expressions

Content Manager ignores case for letters and character sets in regular expressions, so you need not specify both lowercase and uppercase letters in your expressions. For example, if your regular expression includes the character set [a-z], it matches any lowercase character from a to z and any uppercase character from A to Z.

Similarly, if you include literal characters or words in your regular expression, the expression matches both lowercase and uppercase letters, regardless of the case you use. For example, if your expression includes the literal text viagra, it matches VIAGRA, Viagra, ViAgrA, and so on.

Complexity (Component) Limitations

To ensure that content filters with regular expressions do not cause processing delays for your email, Content Manager limits the complexity of regular expressions. A regular expression can have up to 100 components. Content Manager considers each special character, character set, and string of literal characters as a separate component. For example, the regex \Wc[i|!]alis\W contains the following 9 components: \W , c , [ , i , | , ! , ] , alis , \W

Regex Character Limitations

Content Manager does not support regular expressions with the following special characters, because they could cause delays in processing your email:

*
* (asterisk): Matches 0 or more occurrences of the preceding item
*
+ (plus sign): Matches 1 or more occurrences of the preceding item
Range Limitations:

Content Manager does not support the following types of ranges, because they could cause delays in processing your email:

*
Ranges more than {0,25}
*
A valid range: (a|b|c){0,10}
*
An invalid range: (a|b|c){0,26}
*
An invalid range (equivalent to a *): (a|b|c){1,}
*
Nested ranges
*
A valid range: (a|b|c){5,10}
*
An invalid range: ((a|b|c){0,5}){5,20}
*
An invalid range: ((a|b|c){0,5})?

Examples of Regular Expressions

The following examples illustrate the use and construction of simple regular expressions. Each example includes the type of text to match, one or more regular expressions that match that text, and notes that explain the use of the special characters and formatting.

Important: If you create your own regular expression, use the built-in regex tester to verify that your expression will catch the text you expect and that you haven’t created an overly general expression.

Match Whole Word Only
 
Usage example

Match the word hell. Don’t match the word hello, shell, shellac, and so on.

Regex example

(\W|^)hell(\W|$)

Note
*
\W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching letters before or after the word.

Important: When creating a regex to match whole words, ensure that you include the \W character class, to help to avoid capturing legitimate messages.

*
^ matches the start of a new line. Allows the regex to match the word if it appears at the beginning of a line, with no characters before it.
*
$ matches the end of a line. Allows the regex to match the word if it appears at the the end of a line, with no characters after it.

Note: You can also use the rule filter “matches any word in” to create a rule that matches a specific word. Include the word you want to match in the rule value.

Match Exact Phrase Only
 
Usage example

Match the phrase stock tips.

Regex examples

1: (\W|^)stock\stips(\W|$)

2: (\W|^)stock\s{0,3}tips(\W|$)

3: (\W|^)stock\s{0,3}tip(s){0,1}(\W|$)

Notes
*
\W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the phrase.
*
In example 2, \s matches a space character, and {0,3} indicates that from 0 to 3 spaces can occur between the words stock and tip.
*
^ matches the start of a new line. Allows the regex to match the phrase if it appears at the beginning of a line, with no characters before it.
*
$ matches the end of a line. Allows the regex to match the phrase if it appears at the the end of a line, with no characters after it.
*
In example 3, (s) matches the letter s, and {0,1} indicates that the letter can occur 0 or 1 times after the word tip. Therefore, the regex matches stock tip and stock tips. Alternatively, you can use the character ? instead of {0,1}.
Match Word or Phrase in a List
 
Usage example

Match any word or phrase in the following list:

*
baloney
*
darn
*
drat
*
fooey
*
gosh darnit
*
heck
Regex example
(\W|^)(baloney|darn|drat|fooey|gosh\sdarnit|heck)(\W|$)
Notes
*
(…) groups all the words, such that the \W character class applies to all of the words within the parenthesis.
*
\W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the words or phrases in the list.
*
^ matches the start of a new line. Allows the regex to match the word if it appears at the beginning of a line, with no characters before it.
*
$ matches the end of a line. Allows the regex to match the word if it appears at the the end of a line, with no characters after it
*
| indicates an “or,” so the regex matches any one of the words in the list.
*
\s matches a space character. Use this character to separate words in a phrase.

Note: You can also use the rule filter “matches any word in” to create a rule that matches specific words. The rule filter “matches any word” does not, however, match phrases. Include the words you want to match in the rule value.

Match Word with Different Spellings or Special Characters
 
Usage example

Match the word viagra and some of the obfuscations that spammers use, such as:

*
vi@gra
*
v1agra
*
v1@gra
*
v!@gr@
Regex example

v[i!1][a@]gr[a@]

Notes
*
\W is not included, so that other characters can appear before or after any of the variants of viagra. For example, the regex still matches viagra in the following text:

viagra!! or ***viagra***

*
[i!1] matches the characters i, !, or 1 in the second character postition of the word.

Tip: For information about using regular expressions to match obfuscations for profane words, see Block Messages with Profanity.

Match Word with Variable Characters
 
Usage example

Match any URL that contains the text badmail.com, such as:

*
badmail1.com
*
badmail12.com
*
badmail3.com
*
badmail.junk.com
*
badmail-junk.com
Regex example
badmail(\w.+%\-){0,25}\.com
Notes
*
[\w.+\-] matches any word character (a-z, A-Z, 0-9, or an underscore), a period, a plus sign, a percent sign, or a hyphen. These are the only valid characters in a URL. Note that the \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
*
{0,25} indicates that from 0 to 25 characters in the preceding character set can occur after the text badmail. Content Manager supports matching of up to 25 characters for each character set in a regular expression.
*
The \ before the dash and period “escapes” these characters—that is, it indicates that the dash and period are not a regex special characters themselves. Note that there is no need to escape the period within the square brackets.
Match Any Email Address from a Specific Domain
 
Usage example

Match any email address from the domains yahoo.com, hotmail.com, and gmail.com.

Regex example
(\W|^)[\w.+\-]{0,25}@(yahoo|hotmail|gmail)
\.com(\W|$)
Notes
*
\W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the email address.
*
^ matches the start of a new line. Allows the regex to match the address if it appears at the beginning of a line, with no characters before it.
*
$ matches the end of a line. Allows the regex to match the address if it appears at the the end of a line, with no characters after it.
*
[\w.\-] matches any word character (a-z, A-Z, 0-9, or an underscore), a period, a plus sign, or a hyphen. These are the most commonly used valid characters in the first part of an email address. Note that the \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
*
The \ before the dash and period “escapes” these characters—that is, it indicates that the dash and period are not a regex special characters themselves. Note that there is no need to escape the period within the square brackets.
*
{0,25} indicates that from 0 to 25 characters in the preceding character set can occur before the @ symbol. Content Manager supports matching of up to 25 characters for each character set in a regular expression.
*
The (…) formatting groups the domains, and the | character that separates them indicates an “or.”
Match Any IP Address in a Range
 
Usage example
Match any IP address within the range 192.168.1.0 to 192.168.1.255.
Regex examples

1: 192\.168\.1\.

2: 192\.168\.1\.\d{1,3}

Notes
*
The \ before each period “escapes” the period—that is, it indicates that the period is not a regex special character itself.
*
In the example 1, no characters follow the last period, so the regex matches any IP address beginning with 192.168.1., regardless of the number that follows.
*
In example 2, \d matches any digit from 0 to 9 after the last period, and {1,3} indicates that the from 1 to 3 digits can appear after that last period. In this case, the regex matches any complete IP address beginning with 192.168.1.. Note that this regex also matches invalid IP addresses, such as 192.168.1.999.
Match an Alphanumeric Format
 
Usage example

Match the purchase order numbers for your company. This number has various possible formats, such as:

*
PO nn-nnnnn
*
PO-nn-nnnn
*
PO# nn nnnn
*
PO#nn-nnnn
*
PO nnnnnn
Regex example
(\W|^)po[#\-]{0,1}\s{0,1}\d{2}[\s-]{0,1}\d{4}(\W|$)
Notes
*
\W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the number.
*
^ matches the start of a new line. Allows the regex to match the number if it appears at the beginning of a line, with no characters before it.
*
$ matches the end of a line. Allows the regex to match the number if it appears at the the end of a line, with no characters after it.
*
[#\-] matches a pound sign or a hyphen after the letters po, and {0,1} indicates that one of those characters can occur zero or one times. Note that the \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
*
\s matches a space, and {0,1} indicates that a space can occur zero or one times.
*
\d matches any digit from 0 to 9, and {2} indicates that exactly 2 digits must appear in this position in the number.

SOURCE: https://www.google.com/support/enterprise/static/postini/docs/admin/en/admin_ee_cu/cm_regex.html

 

Regular Expressions

Programming tools and techniques survive and spread in a chaotic, evolutionary way. It’s not always the pretty or brilliant ones that win but rather the ones that function well enough within the right niche—for example, by being integrated with another successful piece of technology.

In this chapter, I will discuss one such tool, regular expressions. Regular expressions are a way to describe patterns in string data. They form a small, separate language that is part of JavaScript and many other languages and tools.

Regular expressions are both terribly awkward and extremely useful. Their syntax is cryptic, and the programming interface JavaScript provides for them is clumsy. But they are a powerful tool for inspecting and processing strings. Properly understanding regular expressions will make you a more effective programmer.

Creating a regular expression

A regular expression is a type of object. It can either be constructed with theRegExp constructor or written as a literal value by enclosing the pattern in forward slash (/) characters.

edit & run code by clicking it
var re1 = new RegExp("abc");
var re2 = /abc/;

Both of these regular expression objects represent the same pattern: an acharacter followed by a b followed by a c.

When using the RegExp constructor, the pattern is written as a normal string, so the usual rules apply for backslashes.

The second notation, where the pattern appears between slash characters, treats backslashes somewhat differently. First, since a forward slash ends the pattern, we need to put a backslash before any forward slash that we want to be part of the pattern. In addition, backslashes that aren’t part of special character codes (like \n) will be preserved, rather than ignored as they are in strings, and change the meaning of the pattern. Some characters, such as question marks and plus signs, have special meanings in regular expressions and must be preceded by a backslash if they are meant to represent the character itself.

var eighteenPlus = /eighteen\+/;

Knowing precisely what characters to backslash-escape when writing regular expressions requires you to know every character with a special meaning. For the time being, this may not be realistic, so when in doubt, just put a backslash before any character that is not a letter, number, or whitespace.

Testing for matches

Regular expression objects have a number of methods. The simplest one istest. If you pass it a string, it will return a Boolean telling you whether the string contains a match of the pattern in the expression.

console.log(/abc/.test("abcde"));
// → true
console.log(/abc/.test("abxde"));
// → false

A regular expression consisting of only nonspecial characters simply represents that sequence of characters. If abc occurs anywhere in the string we are testing against (not just at the start), test will return true.

Matching a set of characters

Finding out whether a string contains abc could just as well be done with a call to indexOf. Regular expressions allow us to go beyond that and express more complicated patterns.

Say we want to match any number. In a regular expression, putting a set of characters between square brackets makes that part of the expression match any of the characters between the brackets.

Both of the following expressions match all strings that contain a digit:

console.log(/[0123456789]/.test("in 1992"));
// → true
console.log(/[0-9]/.test("in 1992"));
// → true

Within square brackets, a dash (-) between two characters can be used to indicate a range of characters, where the ordering is determined by the character’s Unicode number. Characters 0 to 9 sit right next to each other in this ordering (codes 48 to 57), so [0-9] covers all of them and matches any digit.

There are a number of common character groups that have their own built-in shortcuts. Digits are one of them: \d means the same thing as [0-9].

\d Any digit character
\w An alphanumeric character (“word character”)
\s Any whitespace character (space, tab, newline, and similar)
\D A character that is not a digit
\W A nonalphanumeric character
\S A nonwhitespace character
. Any character except for newline

So you could match a date and time format like 30-01-2003 15:20 with the following expression:

var dateTime = /\d\d-\d\d-\d\d\d\d \d\d:\d\d/;
console.log(dateTime.test("30-01-2003 15:20"));
// → true
console.log(dateTime.test("30-jan-2003 15:20"));
// → false

That looks completely awful, doesn’t it? It has way too many backslashes, producing background noise that makes it hard to spot the actual pattern expressed. We’ll see a slightly improved version of this expression later.

These backslash codes can also be used inside square brackets. For example,[\d.] means any digit or a period character. But note that the period itself, when used between square brackets, loses its special meaning. The same goes for other special characters, such as +.

To invert a set of characters—that is, to express that you want to match any character except the ones in the set—you can write a caret (^) character after the opening bracket.

var notBinary = /[^01]/;
console.log(notBinary.test("1100100010100110"));
// → false
console.log(notBinary.test("1100100010200110"));
// → true

Repeating parts of a pattern

We now know how to match a single digit. What if we want to match a whole number—a sequence of one or more digits?

When you put a plus sign (+) after something in a regular expression, it indicates that the element may be repeated more than once. Thus, /\d+/matches one or more digit characters.

console.log(/'\d+'/.test("'123'"));
// → true
console.log(/'\d+'/.test("''"));
// → false
console.log(/'\d*'/.test("'123'"));
// → true
console.log(/'\d*'/.test("''"));
// → true

The star (*) has a similar meaning but also allows the pattern to match zero times. Something with a star after it never prevents a pattern from matching—it’ll just match zero instances if it can’t find any suitable text to match.

A question mark makes a part of a pattern “optional”, meaning it may occur zero or one time. In the following example, the u character is allowed to occur, but the pattern also matches when it is missing.

var neighbor = /neighbou?r/;
console.log(neighbor.test("neighbour"));
// → true
console.log(neighbor.test("neighbor"));
// → true

To indicate that a pattern should occur a precise number of times, use curly braces. Putting {4} after an element, for example, requires it to occur exactly four times. It is also possible to specify a range this way: {2,4} means the element must occur at least twice and at most four times.

Here is another version of the date and time pattern that allows both single- and double-digit days, months, and hours. It is also slightly more readable.

var dateTime = /\d{1,2}-\d{1,2}-\d{4} \d{1,2}:\d{2}/;
console.log(dateTime.test("30-1-2003 8:45"));
// → true

You can also specify open-ended ranges when using curly braces by omitting the number on either side of the comma. So {,5} means zero to five times, and {5,} means five or more times.

Grouping subexpressions

To use an operator like * or + on more than one element at a time, you can use parentheses. A part of a regular expression that is enclosed in parentheses counts as a single element as far as the operators following it are concerned.

var cartoonCrying = /boo+(hoo+)+/i;
console.log(cartoonCrying.test("Boohoooohoohooo"));
// → true

The first and second + characters apply only to the second o in boo and hoo, respectively. The third + applies to the whole group (hoo+), matching one or more sequences like that.

The i at the end of the expression in the previous example makes this regular expression case insensitive, allowing it to match the uppercase B in the input string, even though the pattern is itself all lowercase.

Matches and groups

The test method is the absolute simplest way to match a regular expression. It tells you only whether it matched and nothing else. Regular expressions also have an exec (execute) method that will return null if no match was found and return an object with information about the match otherwise.

var match = /\d+/.exec("one two 100");
console.log(match);
// → ["100"]
console.log(match.index);
// → 8

An object returned from exec has an index property that tells us where in the string the successful match begins. Other than that, the object looks like (and in fact is) an array of strings, whose first element is the string that was matched—in the previous example, this is the sequence of digits that we were looking for.

String values have a match method that behaves similarly.

console.log("one two 100".match(/\d+/));
// → ["100"]

When the regular expression contains subexpressions grouped with parentheses, the text that matched those groups will also show up in the array. The whole match is always the first element. The next element is the part matched by the first group (the one whose opening parenthesis comes first in the expression), then the second group, and so on.

var quotedText = /'([^']*)'/;
console.log(quotedText.exec("she said 'hello'"));
// → ["'hello'", "hello"]

When a group does not end up being matched at all (for example, when followed by a question mark), its position in the output array will holdundefined. Similarly, when a group is matched multiple times, only the last match ends up in the array.

console.log(/bad(ly)?/.exec("bad"));
// → ["bad", undefined]
console.log(/(\d)+/.exec("123"));
// → ["123", "3"]

Groups can be useful for extracting parts of a string. If we don’t just want to verify whether a string contains a date but also extract it and construct an object that represents it, we can wrap parentheses around the digit patterns and directly pick the date out of the result of exec.

But first, a brief detour, in which we discuss the preferred way to store date and time values in JavaScript.

The date type

JavaScript has a standard object type for representing dates—or rather, points in time. It is called Date. If you simply create a date object using new, you get the current date and time.

console.log(new Date());
// → Wed Dec 04 2013 14:24:57 GMT+0100 (CET)

You can also create an object for a specific time.

console.log(new Date(2009, 11, 9));
// → Wed Dec 09 2009 00:00:00 GMT+0100 (CET)
console.log(new Date(2009, 11, 9, 12, 59, 59, 999));
// → Wed Dec 09 2009 12:59:59 GMT+0100 (CET)

JavaScript uses a convention where month numbers start at zero (so December is 11), yet day numbers start at one. This is confusing and silly. Be careful.

The last four arguments (hours, minutes, seconds, and milliseconds) are optional and taken to be zero when not given.

Timestamps are stored as the number of milliseconds since the start of 1970, using negative numbers for times before 1970 (following a convention set by “Unix time”, which was invented around that time). The getTime method on a date object returns this number. It is big, as you can imagine.

console.log(new Date(2013, 11, 19).getTime());
// → 1387407600000
console.log(new Date(1387407600000));
// → Thu Dec 19 2013 00:00:00 GMT+0100 (CET)

If you give the Date constructor a single argument, that argument is treated as such a millisecond count. You can get the current millisecond count by creating a new Date object and calling getTime on it but also by calling theDate.now function.

Date objects provide methods like getFullYear, getMonth, getDate,getHours, getMinutes, and getSeconds to extract their components. There’s also getYear, which gives you a rather useless two-digit year value (such as 93 or 14).

Putting parentheses around the parts of the expression that we are interested in, we can now easily create a date object from a string.

function findDate(string) {
  var dateTime = /(\d{1,2})-(\d{1,2})-(\d{4})/;
  var match = dateTime.exec(string);
  return new Date(Number(match[3]),
                  Number(match[2]) - 1,
                  Number(match[1]));
}
console.log(findDate("30-1-2003"));
// → Thu Jan 30 2003 00:00:00 GMT+0100 (CET)

Word and string boundaries

Unfortunately, findDate will also happily extract the nonsensical date 00-1-3000 from the string "100-1-30000". A match may happen anywhere in the string, so in this case, it’ll just start at the second character and end at the second-to-last character.

If we want to enforce that the match must span the whole string, we can add the markers ^ and $. The caret matches the start of the input string, while the dollar sign matches the end. So, /^\d+$/ matches a string consisting entirely of one or more digits, /^!/ matches any string that starts with an exclamation mark, and /x^/ does not match any string (there cannot be an x before the start of the string).

If, on the other hand, we just want to make sure the date starts and ends on a word boundary, we can use the marker \b. A word boundary can be the start or end of the string or any point in the string that has a word character (as in\w) on one side and a nonword character on the other.

console.log(/cat/.test("concatenate"));
// → true
console.log(/\bcat\b/.test("concatenate"));
// → false

Note that a boundary marker doesn’t represent an actual character. It just enforces that the regular expression matches only when a certain condition holds at the place where it appears in the pattern.

Choice patterns

Say we want to know whether a piece of text contains not only a number but a number followed by one of the words pig, cow, or chicken, or any of their plural forms.

We could write three regular expressions and test them in turn, but there is a nicer way. The pipe character (|) denotes a choice between the pattern to its left and the pattern to its right. So I can say this:

var animalCount = /\b\d+ (pig|cow|chicken)s?\b/;
console.log(animalCount.test("15 pigs"));
// → true
console.log(animalCount.test("15 pigchickens"));
// → false

Parentheses can be used to limit the part of the pattern that the pipe operator applies to, and you can put multiple such operators next to each other to express a choice between more than two patterns.

The mechanics of matching

Regular expressions can be thought of as flow diagrams. This is the diagram for the livestock expression in the previous example:

Visualization of /\b\d+ (pig|cow|chicken)s?\b/

Our expression matches a string if we can find a path from the left side of the diagram to the right side. We keep a current position in the string, and every time we move through a box, we verify that the part of the string after our current position matches that box.

So if we try to match "the 3 pigs" with our regular expression, our progress through the flow chart would look like this:

  • At position 4, there is a word boundary, so we can move past the first box.
  • Still at position 4, we find a digit, so we can also move past the second box.
  • At position 5, one path loops back to before the second (digit) box, while the other moves forward through the box that holds a single space character. There is a space here, not a digit, so we must take the second path.
  • We are now at position 6 (the start of “pigs”) and at the three-way branch in the diagram. We don’t see “cow” or “chicken” here, but we do see “pig”, so we take that branch.
  • At position 9, after the three-way branch, one path skips the s box and goes straight to the final word boundary, while the other path matches ans. There is an s character here, not a word boundary, so we go through thes box.
  • We’re at position 10 (the end of the string) and can match only a word boundary. The end of a string counts as a word boundary, so we go through the last box and have successfully matched this string.

Conceptually, a regular expression engine looks for a match in a string as follows: it starts at the start of the string and tries a match there. In this case, there is a word boundary there, so it’d get past the first box—but there is no digit, so it’d fail at the second box. Then it moves on to the second character in the string and tries to begin a new match there… and so on, until it finds a match or reaches the end of the string and decides that there really is no match.

Backtracking

The regular expression /\b([01]+b|\d+|[\da-f]+h)\b/ matches either a binary number followed by a b, a regular decimal number with no suffix character, or a hexadecimal number (that is, base 16, with the letters a to fstanding for the digits 10 to 15) followed by an h. This is the corresponding diagram:

Visualization of /\b([01]+b|\d+|[\da-f]+h)\b/

When matching this expression, it will often happen that the top (binary) branch is entered even though the input does not actually contain a binary number. When matching the string "103", for example, it becomes clear only at the 3 that we are in the wrong branch. The string does match the expression, just not the branch we are currently in.

So the matcher backtracks. When entering a branch, it remembers its current position (in this case, at the start of the string, just past the first boundary box in the diagram) so that it can go back and try another branch if the current one does not work out. For the string "103", after encountering the 3 character, it will start trying the branch for decimal numbers. This one matches, so a match is reported after all.

The matcher stops as soon as it finds a full match. This means that if multiple branches could potentially match a string, only the first one (ordered by where the branches appear in the regular expression) is used.

Backtracking also happens for repetition operators like + and *. If you match/^.*x/ against "abcxe", the .* part will first try to consume the whole string. The engine will then realize that it needs an x to match the pattern. Since there is no x past the end of the string, the star operator tries to match one character less. But the matcher doesn’t find an x after abcx either, so it backtracks again, matching the star operator to just abc. Now it finds an xwhere it needs it and reports a successful match from positions 0 to 4.

It is possible to write regular expressions that will do a lot of backtracking. This problem occurs when a pattern can match a piece of input in many different ways. For example, if we get confused while writing a binary-number regular expression, we might accidentally write something like /([01]+)+b/.

Visualization of /([01]+)+b/

If that tries to match some long series of zeros and ones with no trailing bcharacter, the matcher will first go through the inner loop until it runs out of digits. Then it notices there is no b, so it backtracks one position, goes through the outer loop once, and gives up again, trying to backtrack out of the inner loop once more. It will continue to try every possible route through these two loops. This means the amount of work doubles with each additional character. For even just a few dozen characters, the resulting match will take practically forever.

The replace method

String values have a replace method, which can be used to replace part of the string with another string.

console.log("papa".replace("p", "m"));
// → mapa

The first argument can also be a regular expression, in which case the first match of the regular expression is replaced. When a g option (for global) is added to the regular expression, all matches in the string will be replaced, not just the first.

console.log("Borobudur".replace(/[ou]/, "a"));
// → Barobudur
console.log("Borobudur".replace(/[ou]/g, "a"));
// → Barabadar

It would have been sensible if the choice between replacing one match or all matches was made through an additional argument to replace or by providing a different method, replaceAll. But for some unfortunate reason, the choice relies on a property of the regular expression instead.

The real power of using regular expressions with replace comes from the fact that we can refer back to matched groups in the replacement string. For example, say we have a big string containing the names of people, one name per line, in the format Lastname, Firstname. If we want to swap these names and remove the comma to get a simple Firstname Lastname format, we can use the following code:

console.log(
  "Hopper, Grace\nMcCarthy, John\nRitchie, Dennis"
    .replace(/([\w ]+), ([\w ]+)/g, "$2 $1"));
// → Grace Hopper
//   John McCarthy
//   Dennis Ritchie

The $1 and $2 in the replacement string refer to the parenthesized groups in the pattern. $1 is replaced by the text that matched against the first group, $2by the second, and so on, up to $9. The whole match can be referred to with$&.

It is also possible to pass a function, rather than a string, as the second argument to replace. For each replacement, the function will be called with the matched groups (as well as the whole match) as arguments, and its return value will be inserted into the new string.

Here’s a simple example:

var s = "the cia and fbi";
console.log(s.replace(/\b(fbi|cia)\b/g, function(str) {
  return str.toUpperCase();
}));
// → the CIA and FBI

And here’s a more interesting one:

var stock = "1 lemon, 2 cabbages, and 101 eggs";
function minusOne(match, amount, unit) {
  amount = Number(amount) - 1;
  if (amount == 1) // only one left, remove the 's'
    unit = unit.slice(0, unit.length - 1);
  else if (amount == 0)
    amount = "no";
  return amount + " " + unit;
}
console.log(stock.replace(/(\d+) (\w+)/g, minusOne));
// → no lemon, 1 cabbage, and 100 eggs

This takes a string, finds all occurrences of a number followed by an alphanumeric word, and returns a string wherein every such occurrence is decremented by one.

The (\d+) group ends up as the amount argument to the function, and the(\w+) group gets bound to unit. The function converts amount to a number—which always works, since it matched \d+—and makes some adjustments in case there is only one or zero left.

Greed

It isn’t hard to use replace to write a function that removes all comments from a piece of JavaScript code. Here is a first attempt:

function stripComments(code) {
  return code.replace(/\/\/.*|\/\*[^]*\*\//g, "");
}
console.log(stripComments("1 + /* 2 */3"));
// → 1 + 3
console.log(stripComments("x = 10;// ten!"));
// → x = 10;
console.log(stripComments("1 /* a */+/* b */ 1"));
// → 1  1

The part before the or operator simply matches two slash characters followed by any number of non-newline characters. The part for multiline comments is more involved. We use [^] (any character that is not in the empty set of characters) as a way to match any character. We cannot just use a dot here because block comments can continue on a new line, and dots do not match the newline character.

But the output of the previous example appears to have gone wrong. Why?

The [^]* part of the expression, as I described in the section on backtracking, will first match as much as it can. If that causes the next part of the pattern to fail, the matcher moves back one character and tries again from there. In the example, the matcher first tries to match the whole rest of the string and then moves back from there. It will find an occurrence of */ after going back four characters and match that. This is not what we wanted—the intention was to match a single comment, not to go all the way to the end of the code and find the end of the last block comment.

Because of this behavior, we say the repetition operators (+, *, ?, and {}) aregreedy, meaning they match as much as they can and backtrack from there. If you put a question mark after them (+?, *?, ??, {}?), they become nongreedy and start by matching as little as possible, matching more only when the remaining pattern does not fit the smaller match.

And that is exactly what we want in this case. By having the star match the smallest stretch of characters that brings us to a */, we consume one block comment and nothing more.

function stripComments(code) {
  return code.replace(/\/\/.*|\/\*[^]*?\*\//g, "");
}
console.log(stripComments("1 /* a */+/* b */ 1"));
// → 1 + 1

A lot of bugs in regular expression programs can be traced to unintentionally using a greedy operator where a nongreedy one would work better. When using a repetition operator, consider the nongreedy variant first.

Dynamically creating RegExp objects

There are cases where you might not know the exact pattern you need to match against when you are writing your code. Say you want to look for the user’s name in a piece of text and enclose it in underscore characters to make it stand out. Since you will know the name only once the program is actually running, you can’t use the slash-based notation.

But you can build up a string and use the RegExp constructor on that. Here’s an example:

var name = "harry";
var text = "Harry is a suspicious character.";
var regexp = new RegExp("\\b(" + name + ")\\b", "gi");
console.log(text.replace(regexp, "_$1_"));
// → _Harry_ is a suspicious character.

When creating the \b boundary markers, we have to use two backslashes because we are writing them in a normal string, not a slash-enclosed regular expression. The second argument to the RegExp constructor contains the options for the regular expression—in this case "gi" for global and case-insensitive.

But what if the name is "dea+hl[]rd" because our user is a nerdy teenager? That would result in a nonsensical regular expression, which won’t actually match the user’s name.

To work around this, we can add backslashes before any character that we don’t trust. Adding backslashes before alphabetic characters is a bad idea because things like \b and \n have a special meaning. But escaping everything that’s not alphanumeric or whitespace is safe.

var name = "dea+hl[]rd";
var text = "This dea+hl[]rd guy is super annoying.";
var escaped = name.replace(/[^\w\s]/g, "\\$&");
var regexp = new RegExp("\\b(" + escaped + ")\\b", "gi");
console.log(text.replace(regexp, "_$1_"));
// → This _dea+hl[]rd_ guy is super annoying.

The search method

The indexOf method on strings cannot be called with a regular expression. But there is another method, search, which does expect a regular expression. Like indexOf, it returns the first index on which the expression was found, or -1 when it wasn’t found.

console.log("  word".search(/\S/));
// → 2
console.log("    ".search(/\S/));
// → -1

Unfortunately, there is no way to indicate that the match should start at a given offset (like we can with the second argument to indexOf), which would often be useful.

The lastIndex property

The exec method similarly does not provide a convenient way to start searching from a given position in the string. But it does provide aninconvenient way.

Regular expression objects have properties. One such property is source, which contains the string that expression was created from. Another property is lastIndex, which controls, in some limited circumstances, where the next match will start.

Those circumstances are that the regular expression must have the global (g) option enabled, and the match must happen through the exec method. Again, a more sane solution would have been to just allow an extra argument to be passed to exec, but sanity is not a defining characteristic of JavaScript’s regular expression interface.

var pattern = /y/g;
pattern.lastIndex = 3;
var match = pattern.exec("xyzzy");
console.log(match.index);
// → 4
console.log(pattern.lastIndex);
// → 5

If the match was successful, the call to exec automatically updates thelastIndex property to point after the match. If no match was found,lastIndex is set back to zero, which is also the value it has in a newly constructed regular expression object.

When using a global regular expression value for multiple exec calls, these automatic updates to the lastIndex property can cause problems. Your regular expression might be accidentally starting at an index that was left over from a previous call.

var digit = /\d/g;
console.log(digit.exec("here it is: 1"));
// → ["1"]
console.log(digit.exec("and now: 1"));
// → null

Another interesting effect of the global option is that it changes the way thematch method on strings works. When called with a global expression, instead of returning an array similar to that returned by exec, match will findall matches of the pattern in the string and return an array containing the matched strings.

console.log("Banana".match(/an/g));
// → ["an", "an"]

So be cautious with global regular expressions. The cases where they are necessary—calls to replace and places where you want to explicitly uselastIndex—are typically the only places where you want to use them.

Looping over matches

A common pattern is to scan through all occurrences of a pattern in a string, in a way that gives us access to the match object in the loop body, by usinglastIndex and exec.

var input = "A string with 3 numbers in it... 42 and 88.";
var number = /\b(\d+)\b/g;
var match;
while (match = number.exec(input))
  console.log("Found", match[1], "at", match.index);
// → Found 3 at 14
//   Found 42 at 33
//   Found 88 at 40

This makes use of the fact that the value of an assignment expression (=) is the assigned value. So by using match = number.exec(input) as the condition in the while statement, we perform the match at the start of each iteration, save its result in a variable, and stop looping when no more matches are found.

Parsing an INI file

To conclude the chapter, we’ll look at a problem that calls for regular expressions. Imagine we are writing a program to automatically harvest information about our enemies from the Internet. (We will not actually write that program here, just the part that reads the configuration file. Sorry to disappoint.) The configuration file looks like this:

searchengine=http://www.google.com/search?q=$1
spitefulness=9.7

; comments are preceded by a semicolon...
; each section concerns an individual enemy
[larry]
fullname=Larry Doe
type=kindergarten bully
website=http://www.geocities.com/CapeCanaveral/11451

[gargamel]
fullname=Gargamel
type=evil sorcerer
outputdir=/home/marijn/enemies/gargamel

The exact rules for this format (which is actually a widely used format, usually called an INI file) are as follows:

  • Blank lines and lines starting with semicolons are ignored.
  • Lines wrapped in [ and ] start a new section.
  • Lines containing an alphanumeric identifier followed by an = character add a setting to the current section.
  • Anything else is invalid.

Our task is to convert a string like this into an array of objects, each with aname property and an array of settings. We’ll need one such object for each section and one for the global settings at the top.

Since the format has to be processed line by line, splitting up the file into separate lines is a good start. We used string.split("\n") to do this inChapter 6. Some operating systems, however, use not just a newline character to separate lines but a carriage return character followed by a newline ("\r\n"). Given that the split method also allows a regular expression as its argument, we can split on a regular expression like /\r?\n/ to split in a way that allows both "\n" and "\r\n" between lines.

function parseINI(string) {
  // Start with an object to hold the top-level fields
  var currentSection = {name: null, fields: []};
  var categories = [currentSection];

  string.split(/\r?\n/).forEach(function(line) {
    var match;
    if (/^\s*(;.*)?$/.test(line)) {
      return;
    } else if (match = line.match(/^\[(.*)\]$/)) {
      currentSection = {name: match[1], fields: []};
      categories.push(currentSection);
    } else if (match = line.match(/^(\w+)=(.*)$/)) {
      currentSection.fields.push({name: match[1],
                                  value: match[2]});
    } else {
      throw new Error("Line '" + line + "' is invalid.");
    }
  });

  return categories;
}

This code goes over every line in the file, updating the “current section” object as it goes along. First, it checks whether the line can be ignored, using the expression /^\s*(;.*)?$/. Do you see how it works? The part between the parentheses will match comments, and the ? will make sure it also matches lines containing only whitespace.

If the line is not a comment, the code then checks whether the line starts a new section. If so, it creates a new current section object, to which subsequent settings will be added.

The last meaningful possibility is that the line is a normal setting, which the code adds to the current section object.

If a line matches none of these forms, the function throws an error.

Note the recurring use of ^ and $ to make sure the expression matches the whole line, not just part of it. Leaving these out results in code that mostly works but behaves strangely for some input, which can be a difficult bug to track down.

The pattern if (match = string.match(...)) is similar to the trick of using an assignment as the condition for while. You often aren’t sure that your call to match will succeed, so you can access the resulting object only inside an if statement that tests for this. To not break the pleasant chain ofif forms, we assign the result of the match to a variable and immediately use that assignment as the test in the if statement.

International characters

Because of JavaScript’s initial simplistic implementation and the fact that this simplistic approach was later set in stone as standard behavior, JavaScript’s regular expressions are rather dumb about characters that do not appear in the English language. For example, as far as JavaScript’s regular expressions are concerned, a “word character” is only one of the 26 characters in the Latin alphabet (uppercase or lowercase) and, for some reason, the underscore character. Things like é or β, which most definitely are word characters, will not match \w (and will match uppercase \W, the nonword category).

By a strange historical accident, \s (whitespace) does not have this problem and matches all characters that the Unicode standard considers whitespace, including things like the nonbreaking space and the Mongolian vowel separator.

Some regular expression implementations in other programming languages have syntax to match specific Unicode character categories, such as “all uppercase letters”, “all punctuation”, or “control characters”. There are plans to add support for such categories JavaScript, but it unfortunately looks like they won’t be realized in the near future.

Summary

Regular expressions are objects that represent patterns in strings. They use their own syntax to express these patterns.

/abc/ A sequence of characters
/[abc]/ Any character from a set of characters
/[^abc]/ Any character not in a set of characters
/[0-9]/ Any character in a range of characters
/x+/ One or more occurrences of the pattern x
/x+?/ One or more occurrences, nongreedy
/x*/ Zero or more occurrences
/x?/ Zero or one occurrence
/x{2,4}/ Between two and four occurrences
/(abc)/ A group
/a|b|c/ Any one of several patterns
/\d/ Any digit character
/\w/ An alphanumeric character (“word character”)
/\s/ Any whitespace character
/./ Any character except newlines
/\b/ A word boundary
/^/ Start of input
/$/ End of input

A regular expression has a method test to test whether a given string matches it. It also has an exec method that, when a match is found, returns an array containing all matched groups. Such an array has an index property that indicates where the match started.

Strings have a match method to match them against a regular expression and a search method to search for one, returning only the starting position of the match. Their replace method can replace matches of a pattern with a replacement string. Alternatively, you can pass a function to replace, which will be used to build up a replacement string based on the match text and matched groups.

Regular expressions can have options, which are written after the closing slash. The i option makes the match case insensitive, while the g option makes the expression global, which, among other things, causes the replacemethod to replace all instances instead of just the first.

The RegExp constructor can be used to create a regular expression value from a string.

Regular expressions are a sharp tool with an awkward handle. They simplify some tasks tremendously but can quickly become unmanageable when applied to complex problems. Part of knowing how to use them is resisting the urge to try to shoehorn things that they cannot sanely express into them.

Exercises

It is almost unavoidable that, in the course of working on these exercises, you will get confused and frustrated by some regular expression’s inexplicable behavior. Sometimes it helps to enter your expression into an online tool likedebuggex.com to see whether its visualization corresponds to what you intended and to experiment with the way it responds to various input strings.

Regexp golf

Code golf is a term used for the game of trying to express a particular program in as few characters as possible. Similarly, regexp golf is the practice of writing as tiny a regular expression as possible to match a given pattern, and only that pattern.

For each of the following items, write a regular expression to test whether any of the given substrings occur in a string. The regular expression should match only strings containing one of the substrings described. Do not worry about word boundaries unless explicitly mentioned. When your expression works, see whether you can make it any smaller.

  1. car and cat
  2. pop and prop
  3. ferret, ferry, and ferrari
  4. Any word ending in ious
  5. A whitespace character followed by a dot, comma, colon, or semicolon
  6. A word longer than six letters
  7. A word without the letter e

Refer to the table in the chapter summary for help. Test each solution with a few test strings.

// Fill in the regular expressions

verify(/.../,
       ["my car", "bad cats"],
       ["camper", "high art"]);

verify(/.../,
       ["pop culture", "mad props"],
       ["plop"]);

verify(/.../,
       ["ferret", "ferry", "ferrari"],
       ["ferrum", "transfer A"]);

verify(/.../,
       ["how delicious", "spacious room"],
       ["ruinous", "consciousness"]);

verify(/.../,
       ["bad punctuation ."],
       ["escape the dot"]);

verify(/.../,
       ["hottentottententen"],
       ["no", "hotten totten tenten"]);

verify(/.../,
       ["red platypus", "wobbling nest"],
       ["earth bed", "learning ape"]);


function verify(regexp, yes, no) {
  // Ignore unfinished exercises
  if (regexp.source == "...") return;
  yes.forEach(function(s) {
    if (!regexp.test(s))
      console.log("Failure to match '" + s + "'");
  });
  no.forEach(function(s) {
    if (regexp.test(s))
      console.log("Unexpected match for '" + s + "'");
  });
}

Quoting style

Imagine you have written a story and used single quotation marks throughout to mark pieces of dialogue. Now you want to replace all the dialogue quotes with double quotes, while keeping the single quotes used in contractions likearen’t.

Think of a pattern that distinguishes these two kinds of quote usage and craft a call to the replace method that does the proper replacement.

var text = "'I'm the cook,' he said, 'it's my job.'";
// Change this call.
console.log(text.replace(/A/g, "B"));
// → "I'm the cook," he said, "it's my job."

Numbers again

A series of digits can be matched by the simple regular expression /\d+/.

Write an expression that matches only JavaScript-style numbers. It must support an optional minus or plus sign in front of the number, the decimal dot, and exponent notation—5e-3 or 1E10— again with an optional sign in front of the exponent. Also note that it is not necessary for there to be digits in front of or after the dot, but the number cannot be a dot alone. That is, .5 and5. are valid JavaScript numbers, but a lone dot isn’t.

// Fill in this regular expression.
var number = /^...$/;

// Tests:
["1", "-1", "+15", "1.55", ".5", "5.", "1.3e2", "1E-4",
 "1e+12"].forEach(function(s) {
  if (!number.test(s))
    console.log("Failed to match '" + s + "'");
});
["1a", "+-1", "1.2.3", "1+1", "1e4.5", ".5.", "1f5",
 "."].forEach(function(s) {
  if (number.test(s))
    console.log("Incorrectly accepted '" + s + "'");
});

First, do not forget the backslash in front of the dot.

Matching the optional sign in front of the number, as well as in front of the exponent, can be done with [+\-]? or (\+|-|) (plus, minus, or nothing).

The more complicated part of the exercise is the problem of matching both"5." and ".5" without also matching ".". For this, a good solution is to use the | operator to separate the two cases—either one or more digits optionally followed by a dot and zero or more digits or a dot followed by one or more digits.

Finally, to make the e case-insensitive, either add an i option to the regular expression or use [eE].

SOURCE: http://eloquentjavascript.net/09_regexp.html

SEE MORE SAMPLE: http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3&AspxAutoDetectCookieSupport=1

 

Posted in JAVASCRIPT | Tagged , | Leave a comment

RESETTING NETWORK CONNECTIONS FOR WINDOWS (DNS ISSUE)

There are many ways to reset network settings on a Windows machine. Below are just a few things you can try to reset settings related to network services. If one method does not fix your problem, try another as there may be multiple things prohibiting your use of the internet.

Basic Troubleshooting
Sometimes network issues may be so simple we overlook the easy solutions
first. Below are some suggestions on what to check before undergoing the more technical steps.

1) If you are connected with an Ethernet cable, try unplugging and re-plugging it.
2) If you are connected wirelessly, try leaving your current Wi-Fi network and re-joining it.

3) Have you registered your devices? ResNet requires each device to be
registered in order to access the network.
4) Make sure your network adapters are enabled. This document shows you
how to check for that below.
5) Try rebooting your computer
6) Do you have viruses prohibiting your use of the internet? Try running a virus
scan with an application like Malwarebytes or AVAST Antivirus.
7) If you connect to a router make sure that it is connected to the wall jack.
8) Routers need to be registered with ResNet in order to use them.
9) If you are using an Ethernet cable, make sure that that cable is not faulty by
testing your connection with another wall jack.

Renewing an IP Address
Step One: Open up Command Prompt. You can find this by going to Start then typing cmd
into the search box. Under Programs, there should be an application named cmd.exe. Left
click this to open up Command Prompt. Right click this to select Run as administrator.
Search for cmd.exe

Step Two: Inside the Command Prompt window, type one of the following commands:

ipconfig –release

-release
OR
ipconfig /release
/release
 
And hit Enter. This command will get rid of your current IP address, which is like a “passport” that allows you into the web.

Step Three: Inside the Command Prompt window, type one of the following commands:

ipconfig -renew

-renew

OR

ipconfig /renew
/renew

And hit Enter. This command will ask your ISP for another IP address.

Step Four: Open a web browser to see if renewing an IP address resolved your issue.

Re-enabling Network Adapters
Step One: Open up Network and Sharing Center. You can find this by going to Start then typing Network and Sharing Center into the search box.
Network and Sharing Search

Step Two: In the left window pane click Change adapter settings.
Change Adapter Settings

Step Three: You will see all of your network adapters here which include the Ethernet and Wireless adapters. 

 
LAN adapter  WAN adapter
 
Depending on what you are trying to fix, choose the appropriate adapter. Usually, Local Area Connection is your Ethernet adapter and Wireless Network Connection is your Wi-Fi adapter. Although, these might be named differently the default names are Local Area Connection and Wireless Network Connection.

Step Four: Disable the adapter that is having issues with internet connectivity either by right clicking the adapter and selecting disable, or double clicking the adapter and choosing the disable button.

Step Five: Re-enable the adapter by double clicking it. You can also re-enable it by right clicking on the adapter icon and selecting Enable.

Step Six: Once the adapters have been re-enabled, try opening a web browser to see if your issue has been resolved.

Flushing DNS
Step One: Open up Command Prompt. You can find this by going to Start then typing cmd into the search box. Under Programs, there should be an application named cmd.exe. Left click this to open up Command Prompt. Right click this to select Run as administrator.

Step Two: Inside the Command Prompt window, type one of the following commands:

ipconfig -flushdns
-flushdns

OR

ipconfig /flushdns
/flushdns

And hit Enter. This command resets your DNS resolver. Basically, what DNS does is record names of websites to their IP addresses. By removing the record that has all the names, the computer can re-establish connections to websites by having to ask for their IP address again.

Resetting TCP/IP Stack

Step One: Open up Command Prompt. You can find this by going to Start then typing cmd into the search box. Under Programs, there should be an application named cmd.exe. Left click this to open up Command Prompt. Right click this to select Run as administrator.

Step Two: Inside the Command Prompt window, type one of the following commands:

netsh int ip reset
netsh int ip reset

And hit Enter. This command will reset whats called the TCP/IP stack, which is what your computer uses to communicate between your device and incoming data. After you hit Enter, some text will appear and may look similar to the image below.

Step Three: Inside Command Prompt, type one of the following commands:

netsh int ip set address name=”Local Area Connection” dhcp
netsh int ipv4 set address name="Housing Static DNS" dhcp

OR FOR WIRELESS

netsh int ipv4 set address name=”Wireless Network Connection” dhcp

And hit Enter. This command will enable DHCP to run through the specified adapter. If DHCP is already enabled it will reply as such.

NOTE: If your adapters are NOT named the same as the above, simply replace the string of text between the quotation marks in the command with the EXACT name as your adapters e.g. I have an adapter named Housing Static DNS, therefore, the command I will type is: netsh int ip set address name=”Housing Static DNS” dhcp. Also, Windows is usually good about differentiating between IPv4 and IPv6 (types of IP addresses). In my example above, I explicitly wrote ipv4 but the command does not need this to execute.

Command Prompt Errors 

If you are receiving errors when trying to run the commands above, give this fix a try below.

Step One: In Command Prompt, type setx path “%path%”;C:\Windows;C:\Windows\System32
setx path "%path%";C:\Windows;C\Windows\System32

Step Two: To verify that you have added the path correctly, type path in Command Prompt. You should be able to see C:\Windows and C:\Windows\System32. The semi-colons denote one complete and explicit path set e.g. C:\Windows and C:\Windows\System32 are two separate paths.
path

Step Three: To verify that netsh is working, type netsh help. This should list all available netsh commands.
netsh help

Posted in WINDOWS | Leave a comment

SOME USEFUL .htaccess TRICKS FOR WORDPRESS

1. Protect Your WordPress Admin Area

You can use .htaccess to protect your WordPress admin area by limiting the access to selected IP addresses only. Simply copy and paste this code into your .htaccess file:

01 AuthUserFile /dev/null
02 AuthGroupFile /dev/null
03 AuthName "WordPress Admin Access Control"
04 AuthType Basic
05 <LIMIT GET>
06 order deny,allow
07 deny from all
08 # whitelist Syed's IP address
09 allow from xx.xx.xx.xxx
10 # whitelist David's IP address
11 allow from xx.xx.xx.xxx
12 # whitelist Amanda's IP address
13 allow from xx.xx.xx.xxx
14 # whitelist Muhammad's IP address
15 allow from xx.xx.xx.xxx
16 # whitelist Work IP address
17 allow from xx.xx.xx.xxx
18 </LIMIT>

Replace xx.xx.xx.xxx with your own IP addresses. If you use more than one IP address to access the internet, then make sure you add them as well. See our guide on how to protect your admin folder in WordPress using .htaccess

2. Password Protect WordPress Admin Folder

Password protect your WordPress admin directory using .htaccess file

First you need to create a .htpasswds file. You can easily create one by using thisonline generator.

Upload this .htpasswds file outside your publicly accessible web directory or /public_html/ folder. A good path would be:

home/user/.htpasswds/public_html/wp-admin/passwd/

Now you need to create a new .htaccess file and add this code:

01 AuthName "Admins Only"
02 AuthUserFile /home/yourdirectory/.htpasswds/public_html/wp-admin/passwd
03 AuthGroupFile /dev/null
04 AuthType basic
05 require user putyourusernamehere
06 <Files admin-ajax.php>
07 Order allow,deny
08 Allow from all
09 Satisfy any
10 </Files>

Important: Don’t forget to replace AuthUserFile path with the file path of your .htpasswds file and add your own username.

Upload this .htaccess file to your wp-admin folder. That’s all, your WordPress admin folder is now password protected and only you or the users you allow will be able to access it. For detailed instructions, take a look at how to password protect your WordPress admin (wp-admin) directory.

3. Disable Directory Browsing in WordPress

Many WordPress security experts recommend disabling directory browsing. With directory browsing enabled, hackers can look into your site’s directory and file structure to find a vulnerable file. Learn more about why and how to disable directory browsing in WordPress.

Disable directory browsing using .htaccess file in WordPress

To disable directory browsing in WordPress all you need to do is add this single line in your .htaccess file:

1 Options -Indexes

4. Disable PHP Execution in Some WordPress Directories

Sometimes hacked WordPress sites usually have backdoor files. These backdoor files are often disguised as core WordPress files and are placed in /wp-includes/ or /wp-content/uploads/ folders. An easier way to improve your WordPress security is by disabling PHP execution for some WordPress directories.

Create a blank .htaccess file and paste this code inside it:

1 <Files *.php>
2 deny from all
3 </Files>

Now upload this file to your /wp-content/uploads/ and /wp-includes/ directories. For more information check out this tutorial on how to disable PHP execution in certain WordPress directories.

5. Protect Your WordPress Configuration wp-config.php File

Probably the most important file in your WordPress website’s root directory is wp-config.php file. It contains information about your WordPress database and how to connect to it. To protect your wp-config.php file from unathorized access, simply add this code to your .htaccess file:

1 <files wp-config.php>
2 order allow,deny
3 deny from all
4 </files>

6. Setting up 301 Redirects Through .htaccess File

Using 301 redirects is the most SEO friendly way to tell your users that a content has moved to a new location. If you want to properly manage your 301 Redirects on posts per post basis then check out how to do 301 redirects in WordPress with Quick Page/Post Redirect.

On the other hand if you just quickly want to redirect users from one URL to another, then all you need to do is paste this code in your .htaccess file

1 Redirect 301 /oldurl/ http://www.example.com/newurl
2 Redirect 301 /category/television/ http://www.example.com/category/tv/

7. Ban Suspicious IP Addresses

Seeing unusual requests from an IP address? Want to block an IP address from accessing your website? Add this code to your .htaccess file:

1 <Limit GET POST>
2 order allow,deny
3 deny from xxx.xxx.xx.x
4 allow from all
5 </Limit>

Replace xxx with the IP address you want to block.

8. Disable Image Hotlinking in WordPress Using .htaccess

Other people can slow down your website and steal your bandwidth by hotlinking images from your website. Normally, this doesn’t concern most users. However, if you run a popular site with lots of images and photos, then hotlinking can become a serious issue. You can prevent image hotlinking by adding this code in your .htaccess file:

1 #disable hotlinking of images with forbidden or custom image option
2 RewriteEngine on
3 RewriteCond %{HTTP_REFERER} !^$
4 RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?wpbeginner.com [NC]
5 RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
6 RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?feeds2.feedburner.com/wpbeginner [NC]
7 RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]

Don’t forget to replace wpbeginner.com with your own domain name.

9. Protect .htaccess From Unauthorized Access

As you have seen that there are so many things that can be done using .htaccess file. Due to the power and control it has on your web server, it is important that you protect it from unauthorized access by hackers. Simply add this code to your .htaccess file:

1 <files ~ "^.*\.([Hh][Tt][Aa])">
2 order allow,deny
3 deny from all
4 satisfy all
5 </files>

We hope this article helped you learn some of the most useful .htaccess tricks for WordPress.

 

SOURCE: http://www.wpbeginner.com/wp-tutorials/9-most-useful-htaccess-tricks-for-wordpress/

Posted in WORDPRESS | Leave a comment

WORDPRESS .HTACCESS FILE EXPLAINED!

WordPress’ .htaccess file explained

If you have configured your WordPress system to use pretty permalinks the following will be added to the .htaccess file:

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Explaining the .htaccess, row by row

<IfModule mod_rewrite.c>
This line checks if the mod_rewrite module is available on the server. If not, none of the enclosed Rewrite commands will be processed.

RewriteEngine On
This directive enables the runtime rewriting engine.

RewriteBase /
Let’s the server know that the .htaccess was reached via / and not through any other path prefix.

RewriteCond %{REQUEST_FILENAME} !-f
This condition is true if the the path REQUEST_FILENAME not refers to an existing file.

RewriteCond %{REQUEST_FILENAME} !-d
This condition is true if the the path REQUEST_FILENAME not refers to an existing directory

RewriteRule . /index.php [L]
If the two RewriteCond’s listed above evaluated to true the server will load index.php. [L] indicates that no further rewrite rules should be processed.
If any of the RewriteCond’s evaluated to false, the server will load the actual file or directory instead of index.php.

SOURCE: http://rogerkar.blogspot.com/2009/04/wordpress-htaccess-file-explained.html
SOURCE TO UNDERSTAND THE SYNTAX ABOVE IN APACHE: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Posted in WORDPRESS | Leave a comment