Regex: ลบเว้นวรรคตั้งแต่สองแท็กขึ้นไประหว่างแท็กเฉพาะและปล่อยให้เว้นวรรคแทน


1

ฉันต้องการลบช่องว่างทุกสองหรือมากกว่าระหว่างแท็กเฉพาะและปล่อยให้มีช่องว่างแทน:

ตัวอย่างเช่น:

<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>

ความปรารถนาของฉันออก:

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

ฉันลองอะไรบางอย่าง แต่ใช้งานไม่ได้

(?<=<p class="text_obisnuit">)\s*|\s*(?=</p>)

คำตอบ:


1

สิ่งนี้จะลบพื้นที่ 2 หรือมากกว่าเท่านั้น ภายใน <p class="text_obisnuit"> และ </p> และเก็บช่องว่างอื่น ๆ อีกหลายช่อง

  • Ctrl + H
  • หาอะไร: (?:<p class="text_obisnuit">|\G)(?:(?!</p>).)*?\s\K\s+
  • แทนที่ด้วย: LEAVE EMPTY
  • ตรวจสอบล้อมรอบ
  • ตรวจสอบการแสดงออกปกติ
  • อย่าตรวจสอบ . matches newline ขึ้นอยู่กับว่าคุณต้องการจับคู่หลายบรรทัดหรือไม่
  • แทนที่ทั้งหมด

คำอธิบาย:

(?:                         # start non capture group
  <p class="text_obisnuit"> # literally
 |                          # OR
  \G                        # restart from position of last match
)                           # end group
(?:                         # start non capture group
  (?!</p>)                  # negative lookahead, make sure we haven't reach </p>
  .                         # any character
)*?                         # group may appear 0 or more times, not greedy
\s                          # a space
\K                          # forget all we have seen until this position
\s+                         # 1 or more spaces

รับข้อความ:

other     text

<p class="text_obisnuit">  The context of articles,   stories, and conversations helps you     figure out and understand the meaning   of English words in the text that are new to you.   </p>

other    text

ตัวอย่างผลลัพธ์ที่ได้รับ:

other     text

<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>

other    text

บันทึก: มันช่วยให้พื้นที่หลังจาก <p...> และก่อนหน้านี้ </p>


หากคุณต้องการลบช่องว่างเหล่านี้คุณต้องเรียกใช้ regex อื่น:

  • Ctrl + H
  • หาอะไร: (?<=<p class="text_obisnuit">)\s+|\s+(?=</p>)
  • แทนที่ด้วย: LEAVE EMPTY
  • ยกเลิกการเลือกกรณีการจับคู่
  • ตรวจสอบล้อมรอบ
  • ตรวจสอบการแสดงออกปกติ
  • แทนที่ทั้งหมด

คำอธิบาย:

(?<=                        # start positive lookbehind, make sure we have 
  <p class="text_obisnuit"> # literally
)                           # end lookbehind
\s+                         # 1 or more spaces
|                           # OR
\s+                         # 1 or more spaces
(?=                         # start positive lookahead
  </p>                      # literally
)                           # end lookahead

ตัวอย่างผลลัพธ์ที่ได้รับ:

other     text

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

other    text

1
สวัสดีโตโต้ นั่นเป็นปัญหาของฉันฉันต้องลบพื้นที่ก่อนและหลัง & lt; p & gt; .. & lt; / p & gt;
Just Me

โตโต้ - ฉันสามารถทำให้ทุกอย่างให้เลือกตรงกลางแท็กเหล่านั้นด้วยสิ่งที่ชอบ ^(<p class="text_obisnuit">)\K.*\s{2,}.+?(?=</p>) ดังนั้นจึงไม่แน่ใจว่าคุณมีความคิดที่จะทำให้ดีขึ้นแล้วจึงแทนที่ช่องว่างทั้ง 2 หรือมากกว่าด้วยอักขระภายใน
Pimp Juice IT

@JustMe: มันเป็นไปไม่ได้ที่จะทำใน regex เพียงอันเดียวเพราะคุณต้องการที่จะลบ 2 หรือมากกว่าช่องว่างที่มี 1 ช่องว่างนั่นเป็นกรณีสำหรับช่องว่างด้านซ้ายหลังจาก <p...>, regex นี้แทนที่ 2 หรือมากกว่าพื้นที่ด้วย 1 ช่องว่าง คุณต้องเรียกใช้ regex อื่นดูการแก้ไขของฉัน
Toto

1
@PimpJuiceIT: การแทนที่ไม่เหมือนกันสำหรับช่องว่างที่หนึ่งและสุดท้ายที่พวกเขาต้องการที่จะลบพวกเขาอย่างสมบูรณ์ แต่ให้ 1 พื้นที่สำหรับช่องว่างอื่น ๆ ที่อยู่ตรงกลางฉันคิดว่ามันเป็นไปไม่ได้ด้วย regex เดียว (หรืออาจจะกลายเป็น ถึงซับซ้อน!)
Toto

0

HTML ไม่ได้อยู่ในความดูแลทั่วไปสำหรับช่องว่าง หากคุณแสดง HTML ของคุณคุณจะเห็นว่าช่องว่างนั้นหายไป

ฉันได้สร้างให้คุณ JSFiddle สำหรับการทดสอบ

วิธีแก้ปัญหาที่ง่ายกว่าคือเพียงแค่แทนที่ช่องว่างสองช่องด้วยช่องว่างหนึ่งช่องและทำซ้ำให้มากที่สุด ครั้งที่เป็นไปได้ แต่ช่องว่างไม่สำคัญจริงๆเว้นแต่ใน ข้อความที่ฟอร์แมตแล้วซึ่งใช้ <pre> Tag.


สวัสดี harrymc มันไม่เกี่ยวกับ html แต่ฉันต้องการที่จะเห็นทุกอย่างในการสั่งซื้อใน Notepad ++ ไม่เป็นระเบียบ
Just Me

ด้วย regex ที่ซับซ้อนมากคุณจะเสี่ยงต่อการทำลายข้อความแทนที่จะทำให้สวยงาม เกี่ยวกับการทำให้สวยงาม: คุณเคยดูปลั๊กอิน XML Tools หรือไม่? มันอาจทำงานได้ดีกว่า regex ในการทำให้ข้อความของคุณอ่านได้ มันใช้งานได้กับ HTML ด้วยเช่นกันไม่ใช่แค่ XML
harrymc

0

หาทางแก้ไข:

ค้นหา:

(?s)(\G|<p class="text_obisnuit">)((?!</p>).)*?\K((?<=>)\h+|\h+(?=<|\h))

แทนที่โดย:

(leave empty)

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.