0
votes

I'm trying to get pattern match for string like the following to convert every line into a list item <li>:

-Give on result
&Second new text
-The third text

Another paragraph without list.

-New list here

In natural language: Match every string that starts with - and ended with the new line sign \n

I tried the following pattern that works fine:

/^([-|-]\w+\s*.*)?\n*$/gum

Of course we can write it simply without the square brackets ^(-\w+\s*.*)?\n*$ but for debugging I used it as described.

In the example above, when I replaces the second - with & to be ^([-|&]\w+\s*.*)?\n*$ It works fine too and it mtaches the the second line of the smaple string. However, I could not able to make it matches - prefixed with white space or suffixed with white space.

I changed the sample string to:

- Give on result
&Second new text
 -The third text

Another paragraph without list.

-New list here

and I tried the following pattern:

/^([-|\- |&| -]\w+\s*.*)?\n*$/gum

However, it failed to match any suffixed or prefixed - with white space.

Here are a live demo for the original working pattern:

2
[-|-] doesn't make much sense. [] defines a character class. e.g. "any of the characters inside the [] can match a single character at one spot. so you're saying "is this char a dash or a pipe or a dash".Marc B
I think you want echo preg_replace('/^-(.*?)$/m', '<li>$1</li>', $string);. Or should the content extend inside the same li if the next line doesnt have a -? It would be useful to have what the output should be..chris85
@MarcB Thank you for your wonderful explanation.SaidbakR

2 Answers

0
votes
^(\h*(?:-|&)\h*\w+\s*.*)\n*$

You can try this.| inside [] has no special meaning.See demo.

https://regex101.com/r/nS2lT4/3

A string may start with whitespace, then it should have either - or & which may have spaces ahead. Then it should have at least one alphanumeric characters which may have space ahead. Then it can have anything or nothing. In the end, it will eat up all the newlines it consume or none if it can't.

1
votes

To my understanding, what you want is having a line that starts with an element e (e being & or -), with element being either prefixed/suffixed by space(s).

^\s*[&-]\s*(.*)$

If you do not want multilines, simply do not use the m modifier.