Skip to main content
Inspiring
March 18, 2013
Answered

need help with a GREP statement

  • March 18, 2013
  • 2 replies
  • 1398 views

I'm trying to select just the x

舌ぽう (舌鋒x) ぜっぽう (sharp) tongue

じょう舌 (饒x舌) じょうぜつ garrulity, loquacity

Its always in parentheses. So I want to LookBehind for a left parenthesis then zero or more characters, and LookAhead for zero or more character followed by a parenthesis.

I thought this would work, but it doesn't: (?<=\(.?)x(?=.?\))

This one will select all the text between the parentheses, but I only want the x: (?<=\().?x.?(?=\))

I also tried this (not sure if you can have two lookbehinds...but it didn't work:

(?<=\()(?<=.?)x(?=.?)(?=\))

I'm out of ideas.

This topic has been closed for replies.
Correct answer maxhodges0

Thanks for the tips. I found an easier way. There is always a space between the Japanese text and the English defintinion so I just get any x before the EN SPACE.

btw do you know if there is a way to get the WIDTH of a selection? like in points or mm?

2 replies

Jongware
Community Expert
Community Expert
March 18, 2013

If the 'x' is always between parentheses, you don't have to check for the opening one -- at least, not 'before'

Try this:

x(?=[^(]*\))

Vamitul
Legend
March 18, 2013

that's tricky. lookbehind does not accept variable length.

the 'easy' way to do it is to do multiple Greps, for each lenght:

(?<=\(.)x(?=.\))

(?<=\(.{2}?)x(?=.\))

(?<=\(.{3})x(?=.\))

etc.

maxhodges0AuthorCorrect answer
Inspiring
March 18, 2013

Thanks for the tips. I found an easier way. There is always a space between the Japanese text and the English defintinion so I just get any x before the EN SPACE.

btw do you know if there is a way to get the WIDTH of a selection? like in points or mm?

Jongware
Community Expert
Community Expert
March 18, 2013

maxhodges0 wrote:

btw do you know if there is a way to get the WIDTH of a selection? like in points or mm?

Sure. (You *are* using a script, aren't you?) Width is can be calculated using the horizontalOffset property of an InsertionPoint (which is slightly different from a 'character', because an IP also can designate the position *after* a character).

"In points or mm" is Not Applicable. For the vast majority of functions, all measurements are in your current units. So if your current unit is millimeters and you want the distance in points, you have to use UnitValue (a function which I tend to avoid at all cost), or simply do the calculation

pts = 72 * mm / 25.4

With InsertionPoints, subtracting the first x offset from the last one, this code will show the width of a selected text:

alert (app.selection[0].insertionPoints[-1].horizontalOffset - app.selection[0].insertionPoints[0].horizontalOffset);