Thursday, September 14, 2023

Regular Expression to find a string included between two characters

 Base string: This is a test string [more or less]

need to get only more or less (without the brackets). 

Easy done:

(?<=\[)(.*?)(?=\])

Technically that's using lookaheads and lookbehinds. See Lookahead and Lookbehind Zero-Width Assertions. The pattern consists of:

  • is preceded by a [ that is not captured (lookbehind);
  • a non-greedy captured group. It's non-greedy to stop at the first ]; and
  • is followed by a ] that is not captured (lookahead).

Alternatively you can just capture what's between the square brackets:

\[(.*?)\]

and return the first captured group instead of the entire match.

Source: https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclud

Related Posts:

  • Photoshop - Export PSD layers to files You can export and save layers as individual files using a variety of formats, including PSD, BMP, JPEG, PDF, Targa, and TIFF. Layers are named automatically as they are saved. You can set options to control the generatio… Read More
  • HTML - prompt text Adding prompt text to INPUT <!DOCTYPE html> <!-- http://dhtmlexamples.com/2010/12/04/adding-prompt-text-to-input-and-textarea-tags-in-html/ --> <html> <body>   <table> <tr>… Read More
  • Jfreechart - Draw Custom Line Draw Line at specific point (Vertical or Horizontal) /*** * draw line at a specific value * @param position */ public void drawLine(double position){ // position is the value on the axis ValueMarker marker = new ValueMarke… Read More
  • Java - Generating Javadocs Using Eclipse Generating Javadocs Using Eclipse                                 The more I use the Eclipse IDE, th… Read More
  • Jfreechart - Tips Set a FormatOverride on the axis for NumberAxis and a DateAxis   XYPlot plot = (XYPlot) chart.getPlot(); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setNumberFormatOverride( new NumberFormat(){ … Read More

0 comments:

Post a Comment