i dont undestand the () in regexp.
eg. what is the difference between these lines:
"/hello/"
"/(hello)/"
i dont undestand the () in regexp.
eg. what is the difference between these lines:
"/hello/"
"/(hello)/"
By wrapping it in ( and ) you can capture it and handle it as a whole. Suppose we have the following text:
hellohello hello!
If I wanted to find the word "hello" in two's, I could do this:
/hellohello/
Or, I could do this:
/(hello){2}/
As you have written it, there is no actual difference between the two examples. But parantheses allow you to apply post-logic to that entire group of characters (e.g. as another poster used as an example, {2} afterwards would state the the string "hello" is typed two times in a row without anything in between - hellohello. Parantheses also allow you to use "or" statements - "/(hello|goodbye)/" would match EITHER hello OR goodbye.
The most powerful use of them however is extracting data from a string, rather than just matching it, it lets you pull the data out of the string and do what you want with it.
e.g. in PHP if you did
preg_replace( "/hello (.+)/i", "hello how are you?", $holder );`
Then $holder[1] will contain ALL the text after "hello ", which in this case would be "how are you?"