Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two regexes

Tags:

regex

I have two regular expressions which are working perfectly fine when use independently.

First  : ^.+\.((jpeg)|(gif)|(png))\s*$ : search for .jpeg,gif or png at the end of url 
Second : ^.+(javax.faces.resource|rfRes).* : search for two url patterns 

I want to combine the above two expressions such that " url ends in any of the image " OR "url has javax.faces.resource or rfRes in its path"

I tried using | operator to join both but it seems its not working like below :

^.+\.((jpeg)|(gif)|(png))\s*$ | ^.+(javax.faces.resource|rfRes).* 

but its not working.

Can anybody please help in joining above two regex ?

like image 260
Atul Avatar asked Jan 09 '23 10:01

Atul


2 Answers

You have extra spaces around the | operator:

Your original regex
^.+\.((jpeg)|(gif)|(png))\s*$ | ^.+(javax.faces.resource|rfRes).*
^.+\.((jpeg)|(gif)|(png))\s*$|^.+(javax.faces.resource|rfRes).*
Fixed regex                  ^
                             |

Your solution will try to match "the end of the string and then a space," or "a space and then the beginning of the string." Remember, whitespace is significant in regexes.

like image 67
tckmn Avatar answered Jan 18 '23 22:01

tckmn


The spaces in your combined expression are erroneous. You are requiring a space after end of line or before beginning of line, which is impossible in line-oriented input.

As a further improvement, you can remove the superfluous "anything" parts of the match, as well as a good number of redundant parentheses.

javax\.faces\.resource|rfRes|\.(jpeg|gif|png)\s*$

Notice also the proper quoting of literal full stop characters (a lone . matches any character).

like image 25
tripleee Avatar answered Jan 18 '23 22:01

tripleee