Skip to main content
Known Participant
June 20, 2017
Question

Fractions in GREP

  • June 20, 2017
  • 2 replies
  • 1766 views

Is there a 'wildcard' or the equivalent through which I can select all fractions in GREP?

This topic has been closed for replies.

2 replies

Inspiring
June 20, 2017

Try this code: (\d+\.\d+)|[/\d]|\p{no}

vinny38
Legend
June 20, 2017

Hi

You should be more specific about the patterns of what you call "fractions".

Something like this: (a certain number of digits)+(a slash)+(a certain number of digits) maybe? Like 450/50

Or do you have separators? 27/1.5 or 27/1,5? Or even 1(space)500/2 or 5,500.50/3

Or maybe something completely different, possibly built with some math font?

Please provide more details, being as specific as possible (screenshots can also help)

Vinny

DativeAuthor
Known Participant
June 20, 2017

Okay so I need to be more specific, what I'm trying to do is find WxH measurements and flip them to HxW.

This is my code so far.

(.+)(~<×~<)(.+)(?=~<in\.)
and change to
$3$2$1

While this finds inches I need to be able to find centimetres as well. This is easy enough to do in two steps but I was hoping there was a more elegant solution with one. The structure of the measurements is always as follows

30 × 29 ⅞ in. (78 × 76 cm)

With the numbers varying obviously.

\d doesn't find fractions in the typeface I'm using because they're an individual glyph rather than composite parts.

I think I need a GREP expression like this (\d+~<(possible fraction))( ~<×~< )(\d~<(possible fraction))(?=~<in\.|~<cm\))

I seem to remember theres a way using a question mark you can say 'this may appear or it may not' so ideally I'd like it to be ?wildcard for fractions.

vinny38
Legend
June 21, 2017

I should also note there's probably a way to do it without having to denote the fractions, I would just need the 'any character' expression to end at in. otherwise this happens.


Hi

To find your fractions, you can use set of Unicode ranges.

Given this site: Searching in Unicode character names for fraction​, there are two sets of fractions that could be used to reach fractions (should be tested):

U00BC to U00BE and U2153 to U215E

Grep query for Unicode range is like this : [\x{Unicode}-\x{Unicode}]

So in your case, [\x{00BC}-\x{00BE}\x{2153}-\x{215E}] should catch your fractions.

Please notice I've also used the Unicode search pattern to get the multiplication sign.

Search:

([\d~<\x{00BC}-\x{00BE}\x{2153}-\x{215E}]+)(~<\x{00D7}~<)([\d~<\x{00BC}-\x{00BE}\x{2153}-\x{215E}]+)(?=~<in\.|~<cm\))

Replace:

$3$2$1

Now, this is very verbose and not very elegant

Maybe Grep enthusiasts (pkahrel​, Obi-wan Kenobi​, and many more) could come up with a better query.

Hope that helps anyway

Vinny