Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Friday, 10 November 2017

How to replace double quoted string with bold


Question: How to replace double quoted string with bold in PHP?

$description='Hello "dude", how are you?';
echo preg_replace('/"([^"]+)"/', '<strong>$1</strong>', $description);


Output
Hello dude, how are you?




Question: How to replace bracket string with italic in PHP?

$description='Hello (dude), how are you?';
echo preg_replace('/\(([^)]+)\)/', '<i>$1</i>', $description);


Output
Hello dude, how are you?



Wednesday, 7 October 2015

Regular Expression Interview Questions and Answers


Regular Expression Interview Questions and Answers


Question: What is the mean of Regular Expressions?
Regular Expressions is a pattern which is used to search string like "Search name start with a" OR "Search name end with z".


Question: What is use of Regular Expressions?
It is used to Search Simple String OR Complex String.
For Example:
Search all name start with a.
Search all names end with a


Question:What the mean of different symbols like ^ $ in Regular Expression?
^a Search all string which start with "a".
a$ Search all string which end with "a".
[a-z0-9]{2,5} there should be 2-5 string which can be a-z and 0-9 characters.
[a-z]{2,5} there should be 2-5 string which are a-z characters.
[0-9]{2,5} there should be 2-5 string which are 0-9 characters.


Question: What is Regular Expression to validate Email?
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$


Question: Write a Regular expression to match line that doesn't contain a word?
To find a word "FINDME" in String.
^((?!FINDME).)*$


Question: How do I remove all non alphanumeric characters from a string?
Suppose you want to remove all character except a-z, 0-9 and "space".
[^a-zA-Z0-9\s] 


Question: Simple regular expression for a decimal with a precision of 2
\d+(\.\d{1,2})?


Thursday, 20 March 2014

Xpath Expression Examples

Xpath Expression Examples

Xpath: It is syntax, used to navigate the elements and their attributes. Its make easy to get the any element or attribute.

XPath Terminology
1. element
2. attribute
3. text
4. namespace
5. processing-instruction
6. comment
7. document nodes

Following are few Xpath Expression and their Description
Xpath Expression Description
./writer All <writer> elements within the current context. Note that this is equivalent to the expression in the next row.
writer All <writer> elements within the current context.
first.name All <first.name> elements within the current context.
/bookStore The document element (<bookStore>) of this document.
//writer All <writer> elements in the document.
book[/bookStore/@specialty=@style] All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookStore> element at the root of the document.
writer/first-name All <first-name> elements that are children of an <writer> element.
bookStore//title All <title> elements one or more levels deep in the <bookStore> element (arbitrary descendants). Note that this is different from the expression in the next row.
bookStore/*/title All <title> elements that are grandchildren of <bookStore> elements.
bookStore//book/excerpt//emph All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookStore> element.
.//title All <title> elements one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required.
writer/* All elements that are the children of <writer> elements.
book/*/lastName All <lastName> elements that are grandchildren of <book> elements.
*/* All grandchildren elements of the current context.
*[@specialty] All elements with the specialty attribute.
@style The style attribute of the current context.
price/@exchange The exchange attribute on <price> elements within the current context.
price/@exchange/total Returns an empty node set, because attributes do not contain element children. This expression is allowed by the XML Path Language (XPath) grammar, but is not strictly valid.
book[@style] All <book> elements with style attributes, of the current context.
book/@style The style attribute for all <book> elements of the current context.
@* All attributes of the current element context.
./first-name All <first-name> elements in the current context node. Note that this is equivalent to the expression in the next row.
first-name All <first-name> elements in the current context node.
writer[1] The first <writer> element in the current context node.
writer[first-name][3] The third <writer> element that has a <first-name> child.
my:book The <book> element from the my namespace.
my:* All elements from the my namespace.
@my:* All attributes from the my namespace (this does not include unqualified attributes on elements from the my namespace).
Note that indexes are relative to the parent. Consider the following data:

 

Xpath Expression Description
x/y[1] The first <y> child of each <x>. This is equivalent to the expression in the next row.
x/y[position() = 1] The first <y> child of each <x>.
(x/y)[1] The first <y> from the entire set of <y> children of <x> elements.
x[1]/y[2] The second <y> child of the first <x>.
 

 

Xpath Expression Description
book[last()] The last <book> element of the current context node.
book/writer[last()] The last <writer> child of each <book> element of the current context node.
(book/writer)[last()] The last <writer> element from the entire set of <writer> children of <book> elements of the current context node.
book[excerpt] All <book> elements that contain at least one <excerpt> element child.
book[excerpt]/title All <title> elements that are children of <book> elements that also contain at least one <excerpt> element child.
book[excerpt]/writer[degree] All <writer> elements that contain at least one <degree> element child, and that are children of <book> elements that also contain at least one <excerpt> element.
book[writer/degree] All <book> elements that contain <writer> children that in turn contain at least one <degree> child.
writer[degree][award] All <writer> elements that contain at least one <degree> element child and at least one <award> element child.
writer[degree and award] All <writer> elements that contain at least one <degree> element child and at least one <award> element child.
writer[(degree or award) and publication] All <writer> elements that contain at least one <degree> or <award> and at least one <publication> as the children
writer[degree and not(publication)] All <writer> elements that contain at least one <degree> element child and that contain no <publication> element children.
writer[not(degree or award) and publication] All <writer> elements that contain at least one <publication> element child and contain neither <degree> nor <award> element children.
writer[lastName = "Bob"] All <writer> elements that contain at least one <lastName> element child with the value Bob.
writer[lastName[1] = "Bob"] All <writer> elements where the first <lastName> child element has the value Bob. Note that this is equivalent to the expression in the next row.
writer[lastName [position()=1]= "Bob"] All <writer> elements where the first <lastName> child element has the value Bob.
degree[@from != "Harvard"] All <degree> elements where the from attribute is not equal to "Harvard".
writer[. = "Matthew Bob"] All <writer> elements whose value is Matthew Bob.
writer[lastName = "Bob" and ../price &gt; 50] All <writer> elements that contain a <lastName> child element whose value is Bob, and a <price> sibling element whose value is greater than 50.
book[position() &lt;= 3] The first three books (1, 2, 3).
writer[not(lastName = "Bob")] All <writer> elements that do no contain <lastName> child elements with the value Bob.
writer[first-name = "Bob"] All <writer> elements that have at least one <first-name> child with the value Bob.
writer[* = "Bob"] all writer elements containing any child element whose value is Bob.
writer[lastName = "Bob" and first-name = "Joe"] All <writer> elements that has a <lastName> child element with the value Bob and a <first-name> child element with the value Joe.
price[@intl = "Canada"] All <price> elements in the context node which have an intl attribute equal to "Canada".
degree[position() &lt; 3] The first two <degree> elements that are children of the context node.
p/text()[2] The second text node in each <p> element in the context node.
ancestor::book[1] The nearest <book> ancestor of the context node.
ancestor::book[writer][1] The nearest <book> ancestor of the context node and this <book> element has an <writer> element as its child.
ancestor::writer[parent::book][1] The nearest <writer> ancestor in the current context and this <writer> element is a child of a <book> element.

Sunday, 27 January 2013

Regular Expressions PHP/Javascript

MetaCharacter: It is special chacter(s) which have special meaning in Regular Expression.

For Example:
*, ., /, \, ? and ^.

Sr
Meta character
Desctiption
1
[ ]
Match any character within [phc]
Means find “p” OR “h” OR “c”
2
-
Range
a-z means a to z(a,b,c, ... z)
0-0 means 0 to 9 (0,1,2,3,4,5,6,7,8,9)
A-Z means A to Z(A,B,C,D ..... Z)
3
^
Caret It means start with a character
For example: ^ab (start with “a”)

Inside the bracket if have opposite meaning.
For example: [^a](must not start with “a”)

4
$
End with character
For Example
Abc$ means end with “c”
5
.
The . (period) means any character(s) in this position,
For example, ph. will find php, php-tutorial and php-tutorial-php but not aphp because it has no following character
6
?
Matches the preceding character 0 or 1 times only.
For example:
colou?r will find both color (0 times) and colour (1 time).
7
*
Matches the preceding character 0 or more times.
For example:
tre* will find tree (2 times) and tread (1 time) and trough (0 times).
8
+
Matches the previous character 1 or more times.
For example:
tre+ will find tree (2 times) and tread (1 time) but NOT trough (0 times).
9
{n}
Preceding character, or character range, n times exactly.
For example:
find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 723-4567 OR 132-3234 OR 214-3433.
10
()
To group a character.
For Example:
(php), will find the “php” only.
11
|
Vertical bar used for OR
(a|c) find either “a” OR “c”
12
\d
any character in the range 0 – 9
13
\D
Any character not in between 0-9
14
\s
Whitespace or tab
15
\S
Not whitespace or tab
16
\w
Alphanumeric characters (a-z, 0-9, A-Z)
17
\W
Not alphanumeric character (a-z, 0-9, A-Z)


Thursday, 19 July 2012

Regular Expression

Regular Expression


Following are some Basic terms of Regular Expression which we should be aware.

1. literal: It is Character(s) OR words which we want to search in regular expression.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in".
                Here "tutorial" is literal.


2. Meta Character: It is special chacter(s) which have special meaning in Regular Expression.
For Example:
                *, ., /, \, ? and ^.


3. Target string: The string in which we want to search the string.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in".
                Here "php-tutorial-php.blogspot.in" is target string.


4. Regular expression: The regex with which we find the string.
For Example:
                we want to search "tutorial" in the "php-tutorial-php.blogspot.in" with regex    /(tutorial)/
                Here "/(tutorial)/" is Regular Expression.





Following are the Regular Expression with matches tested for PHP only.


An Introduction of Regular Expression


In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regexp and regex.
The concept of regular expressions was first popularized by utilities provided by Unix distributions, in particular the editor ed and the filter grep.
Regular Expression  Matche
^A
"A" at the beginning of a line
A$
"A"  at the end of a line
A\^
"A^" anywhere on a line (slash for escape spec. mean of ^) 
\$A
"$A" anywhere on a line
^\^
"^" at the beginning of a line
\$$
"$" at the end of a line
[0]
The character "0"
[0-9]
Any number
[^0-9]
Any character other than a number
[-0-9]
Any number or  "-"
[0-9-]
Any number or  "-"
[^-0-9]
Any character except a number or  "-"
[0-9]\]
Any number followed by a "]"
[0-9-z]
Any number, or any character between 0-9  and "z"
[0-9\-a\]\]
Any number, or - or a
\*
Any line with an asterisk
\\
Any line with a backslash
^\*
Any line starting with an asterisk
^A\*
Any line starting with an "A*"
^AA\*
Any line if it starts with one "AA*"
^A(A)*B
Any line with one or more "A"'s followed by a "B"
e.g.
AB, AAB, AAAB, AAAB
^A{4,8}B
Any line starting with "A" having 4-8 char and followed by B
eg
AAAAB, AAAAAAB, AAAAAAB
^A{4,}B
Any line starting with "A" min 4 character and followed by B
^A{4}B
Any line starting with "AAAAB"
A{4,8}
Any line with "A{4,8}"