I am having one line I have to replace that below is the line
/myhome/ishere/where/are.you.ha
Here I have a live regex
preg_match('/(. \/)[^.] (. \.ha)/', $input_line, $output_array);
it results me live this
/myhome/ishere/where/.you.ha
But I need a answer like this
/myhome/ishere/where/you.ha
Please anyone help me to remove this dot.
CodePudding user response:
You can use
/^(. \/)[^.]*\.(.*\.ha)$/
See the regex demo. Details:
^
- start of a string(. \/)
- Group 1: any one or more chars other than line break chars as many as possible and then/
[^.]*
- zero or more chars other than a.
\.
- a.
char(.*\.ha)
- Group 2: any zero or more chars other than line break chars as many as possible and then.ha
$
- end of a string
CodePudding user response:
You could write the pattern as this, which will give you 2 capture groups that you can use:
(. \/)[^.] \.([^.] \.ha)$
Explanation
(. \/)
Capture group 1, match 1 chars and then the last/
[^.]
Match 1 non dots\.
Match a dot([^.] \.ha)
Match non dots, then.ha
$
End of string
If you use $1$2
in the replacement:
$pattern = "/(. \/)[^.] \.([^.] \.ha)$/";
$s = "/myhome/ishere/where/are.you.ha";
echo preg_replace($pattern, "$1$2", $s);
Output
/myhome/ishere/where/you.ha
Or see a code an example using preg_match with 2 capture groups.
CodePudding user response:
Although I got the answer I was mistaking to put \.
in between
(. /)[^.] \.(. \.ha)
preg_replace('/(. \/)[^.] \.(. \.ha)/', '$1$2', $input_lines);
This is how it works.